比如下面的 BlogOwnerRequirementHandler 实现中如何访问 HttpContext?
public class BlogOwnerRequirementHandler : IAuthorizationRequirement, IAuthorizationHandler
{
public Task HandleAsync(AuthorizationHandlerContext context)
{
}
}
通过 context.Resource 就可以访问 HttpContext
public class BlogOwnerRequirementHandler : IAuthorizationRequirement, IAuthorizationHandler
{
public Task HandleAsync(AuthorizationHandlerContext context)
{
if (context.Resource is HttpContext httpContext)
{
if (httpContext.GetBlogContext().IsBlogOwner)
{
context.Succeed(this);
}
}
return Task.CompletedTask;
}
}
参考:ASP.NET Core Authorization handler that allows request from private IP ranges