Is it possible that we can access dbcontext to get my table data and session in custom Policy-Based Authorization? Anyone can help how to achieve it?
services.AddAuthorization(options =>
{
options.AddPolicy("CheckAuthorize",
policy => policy.Requirements.Add(new CheckAuthorize()));
});
services.AddSingleton<IAuthorizationHandler, CheckAuthorize>();
public class CheckAuthorize : AuthorizationHandler<CheckAuthorize>, IAuthorizationRequirement
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, CheckAuthorize requirement)
{
if () //check session to verify user is logged in or not
{
//redirect to login page
}
else
{
if ()//access dbcontext get data from database table to validate user access
{
//redirect to access denied page
}
}
throw new NotImplementedException();
}
}