The normal way to check that a policy is being fulfilled in ASP.NET Core's authorization system is to setup a policy in ConfigureServices like so:
services.AddAuthorization(conf => {
conf.AddPolicy("UserHasRecentPassport", policy => policy.RequireAssertion(ctx => { return ctx.User.HasRecentPassport(); }));
}
... and then specify it for a controller or action using AuthorizeAttribute, like so:
[Authorize("UserHasRecentPassport")]
public class HomeController : Controller {
public IActionResult Index() {
return View();
}
}
However, I'm writing a tag helper which needs to check whether a particular policy is being met. I therefore need to just check this in code rather than using the AuthorizeAttribute approach, ie. something like:
public override void Process(TagHelperContext context, TagHelperOutput output) {
output.TagName = null;
if (!policyRequirementIsMet("UserHasRecentPassport")) {
output.SuppressOutput();
}
}
Is there any way for me to implement policyRequirementIsMet so it goes to ASP.NET Core and says "tell me whether the policy with name X is met"?