How to add custom roles to ASP.NET Core

前端 未结 3 1652
暗喜
暗喜 2021-02-04 11:11

I\'ve found this answer but it doesn\'t seem to fit in my ASP Net Core project.

Things I am trying to understand:

  • How can I add a custom role. I\'ve even l
3条回答
  •  孤街浪徒
    2021-02-04 11:27

    In addition to Temi's detailed answer, remember to replace

     services.AddDefaultIdentity()
                .AddEntityFrameworkStores();
    

    With

     services.AddIdentity().AddEntityFrameworkStores();
    

    Also, make sure that the types specified in AddIdentity<> is the same as the types called in serviceProvider.GetRequiredService<>

    For the above, our types called in serviceProvider.GetRequiredService<> in Configure.cs would be

     var roleManager = serviceProvider.GetRequiredService>();
     var userManager = serviceProvider.GetRequiredService>();
    

    Another important thing to note is that since CreateRoles(IServiceProvider) (from Temi's answer) is an async method, to call it in Configure method (which return void), you can use CreateRoles(serviceProviderInstance).Wait(); Hope this helps.

提交回复
热议问题