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 looked directly in my MySQL database (table
aspnetroles
), but I don't know what to use asId
andConcurrencyStamp
. - Where do I put the code to seed the database with these new roles: in
Startup
? inRegister
underAccountController
? - How do I tie this new role to a user? I've even looked through the tables and I don't know how to assign the data (there is no
user2role
oraspnetusers.role_id
).
You could do this easily by creating a
CreateRoles
method in your startup class. This helps check if the roles are created, and creates the roles if they aren't; on application startup. Like so.and then you could call the
await CreateRoles(serviceProvider);
method from theConfigure
method in the Startup class. ensure you haveIServiceProvider
as a parameter in theConfigure
class.Edit: If you're using ASP.NET core 2.x, my article here provides a much detailed experience. here
In addition to Temi's detailed answer, remember to replace
With
Also, make sure that the types specified in
AddIdentity<>
is the same as the types called inserviceProvider.GetRequiredService<>
For the above, our types called in
serviceProvider.GetRequiredService<>
in Configure.cs would beAnother important thing to note is that since
CreateRoles(IServiceProvider)
(from Temi's answer) is an async method, to call it inConfigure
method (which return void), you can useCreateRoles(serviceProviderInstance).Wait();
Hope this helps.Adding to Temi's answer and Xavier's comment to it, my experience with this was a little different using asp.net-core-2.
In order to get this working I had to add the
IServiceProvider
as a parameter of theConfigure
method to get an instance of it.