38 lines
1.2 KiB
C#
38 lines
1.2 KiB
C#
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 ();
|
|
}
|
|
}
|
|
}
|
|
} |