D
D
Dmitry2019-05-08 13:26:18
ASP.NET
Dmitry, 2019-05-08 13:26:18

How to make [Authorize(Roles = "Administrator")] work?

I have an MVC web application. Made a reconstruction of the database model as stated in the article: https://docs.microsoft.com/en-us/ef/core/get-start... Reconstructed the tables: AspNetUsers, AspNetUserRoles, AspNetRoles. I created views for them to add, edit, delete, view details. Everything is added and edited. But when you try to set [Authorize(Roles = "Administrator")], the program does not let a single user in, although such a role is in the database and the relationship between the role ID and user ID is also written in the table, while if you leave [Authorize] then everything works , i.e. The user passes the authorization check successfully. But that's why it does not see the distribution of roles. As I understand it, it needs to be registered somewhere, but where and how I will not put my mind to it. What should I do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2019-05-15
@Alarih1

I asked myself, I answered...

services.AddDefaultIdentity<IdentityUser>()
                .AddRoles<IdentityRole>()
                .AddDefaultUI(UIFramework.Bootstrap4)
                .AddEntityFrameworkStores<ApplicationDbContext>();

all you had to do was add it to the Startup.cs class in the ConfigureServices method:.AddRoles<IdentityRole>()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question