Hi I have a custom authentication in my asp.net core mvc project and now I have a problem. First I explain my authentication: this is startup config :
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie(options =>
{
options.LoginPath = "/Login";
options.LogoutPath = "/Logout";
options.AccessDeniedPath = "/AccessDenied";
options.ExpireTimeSpan = TimeSpan.FromDays(60);
});
and this is my permission checker class :
public class PermissionCheckerAttribute : AuthorizeAttribute, IAuthorizationFilter
{
private readonly string _permissionName;
public PermissionCheckerAttribute(string permissionName)
{
_permissionName = permissionName;
}
public void OnAuthorization(AuthorizationFilterContext context)
{
var permissionList = context.HttpContext.User.Claims
.Where(claim => claim.Type == "permission").ToList();
var hasPermission = permissionList.Any(claim => claim.Value == _permissionName);
if (!hasPermission)
{
context.Result = new RedirectResult("/AccessDenied");
}
}
}
and this is a sample of my permissions :
[PermissionChecker(TestControllerPermissions.Create)]
public async Task<IActionResult> Privacy()
{
return View();
}
and when user login I get user permissions from database and store it on claims. all is working fine but the problem is how can i update this ? I mean for instance i am admin and i will change some users permissions to affect that user must logout and login again but this is not what i want. how can i update my user claims base on my database every time? should i check that in my permission checkerClass ? that means i should conncet to database for every permision request? please give me some idias.