Use a statement in ASP.NET CORE to achieve customized authorization attributes
to achieve customized authorization logic. This method no longer exists in AuthorizeAttribute
. bool AuthorizeCore(HttpContextBase httpContext)
AuthorizeAttribute
The current method of using strategy
Define strategy in :
Startup.cs
options.AddPolicy("YourPolicyName", policy => policy.RequireClaim(...));
[Authorize]
Using the customized authorization attributes of the statement
[Authorize(Policy = "YourPolicyName")] public IActionResult Action(...)
How to use examples: IAuthorizationFilter
AuthorizeAttribute
public class ClaimRequirementAttribute : TypeFilterAttribute { public ClaimRequirementAttribute(string claimType, string claimValue) : base(typeof(ClaimRequirementFilter)) { Arguments = new object[] { new Claim(claimType, claimValue) }; } } public class ClaimRequirementFilter : IAuthorizationFilter { private readonly Claim _claim; public ClaimRequirementFilter(Claim claim) { _claim = claim; } public void OnAuthorization(AuthorizationFilterContext context) { bool hasClaim = context.HttpContext.User.Claims.Any(c => c.Type == _claim.Type && c.Value == _claim.Value); if (!hasClaim) { context.Result = new ForbidResult(); } } }
The above is the detailed content of How to Implement Custom Authorization in ASP.NET Core Using Claims?. For more information, please follow other related articles on the PHP Chinese website!