Reputation: 11
I want to add new userID and roleId to AspNetUserRoles
Table.
I searched about it, but still didn't get a correct answer.
AspNetUserRoles
Table is something like this.
|userId | roleId |
|abc-12 | 1 |
|abc-12 | 2 |
|dcs_32 | 2 |
How can create new object of AspNetUserRoles
and add new new userID
and roleId
to AspNetUserRoles
Table and remove a row from AspNetUserRoles
Table?
Upvotes: 0
Views: 1876
Reputation: 111
Assuming you are using the official Identity, you will have the following database tables.
They maybe create by below command
dotnet new webapp --auth Individual -o WebApp1
If you want to modify some thing, like login page layout, you will need Scaffold Identity.
If you want change those tables field, you need this Customize the model.
After add your self custom user and role, the place where IdentityUser was used before must be modified.
UserManager<IdentityUser>
Need change to
UserManager<ApplicationUser>
ApplicationUser is your custom user model
If you didn't change, will show this error.
InvalidOperationException: No service for type ‘Microsoft.AspNetCore.Identity.UserManager [Microsoft.AspNetCore.Identity.IdentityUser]’ has been registered.
Upvotes: 0
Reputation: 2800
Look at the base methods from Microsoft Identity framework.
Like:
await _userManager.AddToRoleAsync(user, role.Name);
The UserManager exposes all the methods you required.
Upvotes: 1
Reputation: 640
In order to create roles you have to use RoleManager. There is method CreateAsync(IdentityRole).
In order to assing role to user, you have to use UserManager. There is method AddToRoleAsync(IdentityUser, role).
Both class are members of Microsoft.AspNetCore.Identity.
private readonly UserManager<IdentityUser> _userManager;
private readonly RoleManager<IdentityRole> _rolemanager;
Upvotes: 0