How to use Roles in ASP.NET Core 2.1?

后端 未结 5 2245
小蘑菇
小蘑菇 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 20:58

    In addition to the answers already provided, despite adding .AddRoles(), I still could not get Authorization when use Authorize(Roles = "Administrator") on my controllers. For some reason,the "role claim doesn't seem to affect IsUserInRole or AuthorizeAttribute with a role name."

    To make use of roles I would suggest that one use the ASP.NET 2.0 way like below:

    services.AddIdentity()
                .AddDefaultUI()
                .AddDefaultTokenProviders()
                .AddEntityFrameworkStores();
    

    This way, you get to use your roles and also get the Identity pages scaffolded for you.

    Refer to this issue on aspnet github: Issue 1813

提交回复
热议问题