Initial Commit ...
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using xCommons.Constants;
|
||||
using xCommons.Extensions;
|
||||
|
||||
namespace xCommons.Attributes {
|
||||
public partial class RequireXPoweredAttribute : AuthorizeAttribute, IAuthorizationFilter {
|
||||
|
||||
private readonly bool ignoreAnonymous;
|
||||
|
||||
public RequireXPoweredAttribute (bool IgnoreAnonymous = true) {
|
||||
ignoreAnonymous = IgnoreAnonymous;
|
||||
}
|
||||
|
||||
public void OnAuthorization (AuthorizationFilterContext context) {
|
||||
//
|
||||
var config = context.GetXAppConfiguration ();
|
||||
|
||||
//
|
||||
var isExistsXPowered = context.HttpContext.Request.Headers
|
||||
.Any (h => h.Key
|
||||
.ToNormalString () == XAuthorization.XPoweredBy.ToNormalString () &&
|
||||
h.Value == config.XPoweredValue);
|
||||
|
||||
//
|
||||
var isAnonymous = context.IsAnonymousAllowed ();
|
||||
var handler = ignoreAnonymous ? isAnonymous ? false : true : true;
|
||||
|
||||
//
|
||||
if (handler &&
|
||||
!isExistsXPowered) {
|
||||
context.Result = new UnauthorizedResult ();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user