Reputation: 58
i am using default identity provided by asp.netcore, i have made a role named admin and have also inserted related data in ASPNetUserRoles table, and on my controller i wrote [Authorize(Roles = "Admin")], but still when i try to access that controller, it says, not authorized. here are the following codes.
i have double checked that userid and roleid is correct
select id from AspNetUsers id 58005002-d0d4-4db1-b0bb-5a474cca8013
select * from AspNetRoles id name normalizedName ConcurrencyStamp c4a56464-53f7-440c-b904-fbc7389ed00a Admin ADMIN 080dbb2c-789e-4e28-95fd-6bc73d8b199e
select * from AspNetUserRoles UserId RoleId 58005002-d0d4-4db1-b0bb-5a474cca8013 c4a56464-53f7-440c-b904-fbc7389ed00a
[Authorize(Roles = "Admin")] public class AdminController : Controller {
// GET: /<controller>/
public IActionResult Index()
{
return View();
}
}
i can access other controllers which are only authorized with no roles, but i am having problem with this.
Upvotes: 0
Views: 98
Reputation: 20116
From this post,try to register RoleManager<IdentityRole>
like
services.AddDefaultIdentity<IdentityUser>()
.AddRoles<IdentityRole>()
.AddRoleManager<RoleManager<IdentityRole>>()
.AddDefaultUI(UIFramework.Bootstrap4)
.AddEntityFrameworkStores<DeadAliveDbContext>();
Upvotes: 0
Reputation: 2288
Try to add roles to your services in Startup.cs
file
public void ConfigureServices(IServiceCollection services)
{
services.AddDefaultIdentity<IdentityUser>()
.AddRoles<IdentityRole>();
}
Upvotes: 0