How to use Roles in ASP.NET Core 2.1?

后端 未结 5 2253
小蘑菇
小蘑菇 2021-02-12 20:31

I\'ve created a test project using:

dotnet new razor --auth Individual --output Test

This creates a Startup.cs that contains:

p         


        
5条回答
  •  故里飘歌
    2021-02-12 21:05

    I also fought with this problem a while ago. I couldn't make roles to work.

    In my project I used ASP.NET Core 2.1 (it could be fixed in 2.2, see link) and also scaffolded some pages.

    After long searches on the internet I found this solution (which is also mentioned above in Issue 1813)

    The solution was for me (as proposed in the article) to add the following line with IUserClaimsPrincipalFactory:

            services.AddScoped, 
                UserClaimsPrincipalFactory>();
    
            services.AddDefaultIdentity()
                .AddRoles()
                .AddRoleManager>()
                .AddDefaultTokenProviders()
                .AddEntityFrameworkStores();
    

提交回复
热议问题