I have a custom authorization handler as below:
public class CanApproveArticlesByOtherAuthorsClaimsHandler : AuthorizationHandler<ManageArticleApprovalRequirement>
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, ManageArticleApprovalRequirement requirement)
{
var httpContext = context.Resource as HttpContext;
if (httpContext == null)
{
return Task.CompletedTask;
}
string loggedInUser = context.User.Claims
.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;
string authorId = (string)httpContext.Request.RouteValues["authorid"];
if (context.User.IsInRole("Admin")
&& context.User.HasClaim(claim => claim.Type == "Article Approver" && claim.Value == "true")
&& loggedInUser.ToLower() != authorId.ToLower())
{
context.Succeed(requirement);
}
return Task.CompletedTask;
}
}
I need to check if this policy is a success when I load edit.cshtml and determine if I need to load a control on the page. This is what I have now, but I want to use the custom policy.
@if ((await authService.AuthorizeAsync(User, null, new[] { new ManageArticleApprovalRequirement() })).Succeeded)
{
<div class="col-sm-3 offset-sm-2">
<input asp-for="@Model.Approved" class="form-check-input" />
<label class="form-check-label" asp-for="@Model.Approved">Approve For Publication</label>
</div>
}
And in Startup.cs
services.AddSingleton<IAuthorizationHandler, CanApproveOnlyArticlesByOtherAuthorsHandler>();
and
services.AddAuthorization(options =>
{
//Other code
options.AddPolicy("ApproveArticlesPolicy", policy => policy.AddRequirements(new ManageArticleApprovalRequirement()));
//Other code;
});