I am using asp.net core razor page I'm spending lot time with this thing that appear easy, but can't find solution.
Create a project and working well, login, register, etc. But Authorization not working with roles. Roles are created and:
But always return Access Denied when trying to access:
Now I create two roles 1-Admin 2-User in the AspNetUserRoles database
and this is my code
[Authorize(Policy = "Admin")]
public class AdminModel : PageModel
{
public void OnGet()
{
}
}
and in startup is
services.AddAuthorization(options =>
{
options.AddPolicy("Admin",
policy => policy.RequireRole("Admin"));
});
the problem is always access denied?
CodePudding user response:
The NormalizedName
of the Role should be upper case - ADMIN. It's best to use the RoleManager.CreateAsync
API to create roles rather than adding them manually to the database.
CodePudding user response:
I was make a mistake it was fill AspNetUserRole and AspNetRole manually, and the right way is to fill auto throw register page.