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
Should be something like
UserManager.AddToRole(user, role)
How do I get tge UserManager?
From the DI for example, inject it
But shouldn't I do it in Program.cs? Where then?
Ah, you want to seed the roles?
No, I've already seeded them
Well, you want to seed the relationships
yeah
You can always just do that manually
UserRoles
is, IIRC, the join table of users to rolesWill the authorization work then?
Why wouldn't it?
The roles and relationships of roles to users are stored in the database
Don't know, it just feels like cheating to me lol
I admit I'm being silly
When the user logs in, the framework fetches user's roles from the DB and saves them as claims
It should work, you're right, I dont see whu not
Then, it can check those claims without hitting the db
ok, I'll insert manually then, thanks