C
C#11mo ago
Gipper

How to assign roles to already created Users in ASP.NET Identity?

In ASP.NET I am trying to attribute Roles to specific users in my Database, I already have the users and the roles created in the AspNetUsers and AspNetRoles, but now I would like to attribute each to the other as I wish. Is it "safe" to do so manually, through the SQL Server Object Explorer? Will that work with the Authorize DataAnnotations later? If not what should I do?
16 Replies
Angius
Angius11mo ago
Should be something like UserManager.AddToRole(user, role)
Gipper
GipperOP11mo ago
How do I get tge UserManager?
Angius
Angius11mo ago
From the DI for example, inject it
Gipper
GipperOP11mo ago
But shouldn't I do it in Program.cs? Where then?
Angius
Angius11mo ago
Ah, you want to seed the roles?
Gipper
GipperOP11mo ago
No, I've already seeded them
Angius
Angius11mo ago
Well, you want to seed the relationships
Gipper
GipperOP11mo ago
yeah
Angius
Angius11mo ago
You can always just do that manually UserRoles is, IIRC, the join table of users to roles
Gipper
GipperOP11mo ago
Will the authorization work then?
Angius
Angius11mo ago
Why wouldn't it? The roles and relationships of roles to users are stored in the database
Gipper
GipperOP11mo ago
Don't know, it just feels like cheating to me lol I admit I'm being silly
Angius
Angius11mo ago
When the user logs in, the framework fetches user's roles from the DB and saves them as claims
Gipper
GipperOP11mo ago
It should work, you're right, I dont see whu not
Angius
Angius11mo ago
Then, it can check those claims without hitting the db
Gipper
GipperOP11mo ago
ok, I'll insert manually then, thanks
Want results from more Discord servers?
Add your server