I have my login handler method. In that method I add claims from db to user.
public async Task<IActionResult> OnPostAsync()
{
var result = await _signInManager.PasswordSignInAsync(LoginModel.UserName,
LoginModel.Password, LoginModel.RememberMe, false);
if (result.Succeeded)
{
var user = await _userManager.FindByNameAsync(LoginModel.UserName);
var claims = await _userManager.GetClaimsAsync(user);
ClaimsIdentity id = new (claims, "ApplicationCookie",
ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(id));
return Redirect("/");
}
return Page();
}
In Startup class I registered my policy like that
services.AddAuthorization(options =>
{
options.AddPolicy("IsAdmin", policy => { policy.RequireClaim("Admin"); });
});
But I can't access to this PageModel when I am in admin account
[Authorize(Policy = "IsAdmin")]
public class UserPanel : PageModel
{}
Please, tell me what I am doing wrong.
question from:
https://stackoverflow.com/questions/65873722/access-denied-in-asp-net-core-by-claims-authorization 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…