apologies if this question seems basic, but I am quite stuck despite alot of googling I am trying to setup an ASP.NET webform project to use roles, I am creating users using the following:
protected void CreateUser_Click(object sender, EventArgs e)
{
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>();
var user = new ApplicationUser() { UserName = Email.Text, Email = Email.Text, FirstName = FirstName.Text, LastName = LastName.Text };
IdentityResult result = manager.Create(user, Password.Text);
if (result.Succeeded)
{
string code = manager.GenerateEmailConfirmationToken(user.Id);
string callbackUrl = IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id, Request);
manager.SendEmail(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" callbackUrl "\">here</a>.");
signInManager.SignIn( user, isPersistent: false, rememberBrowser: false);
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
else
{
ErrorMessage.Text = result.Errors.FirstOrDefault();
}
}
I am struggling to setup the project to use roles and add them to the users, could anyone please direct me in the right direction? A lot of the information available online relates to MVC or .NET Core projects and I cannot seem to implement them to my webform project mainly due to my limited knowledge of .net (new to this). Much appreciate your help
CodePudding user response:
Well, since database roles tend to be a "one time" setup, then I usually just directly open the table in edit mode, and add the few roles I want to use.
If you using SQL server, then just fire up SQL studio, and right click on the table, (choose edit 200 rows option). I then just type in the roles, and that is quite much the end of this.
And, if you using a "attached" database in which asp.net attaches the database (and I tend to only recommend this for development), then view/display the database exploer, and the "view" data option ALSO allows you to edit.
Hence, you see this:
So, we just opened the roles table directly, and then I just enter the few roles I need/want.
So, say now this:
As for the "id", I just again manually type in 1, then 2 and so on. I can't recall ever having more then about 5-8 roles anyway, so it not a big deal, and in 99% of cases not worth the time to write code to add roles to the database.
However, you can add roles this way:
vb:
Dim strRole = "ZooRole"
Using context = New ApplicationDbContext()
Dim MyRoleStore = New RoleStore(Of IdentityRole)(context)
MyRoleStore.CreateAsync(New IdentityRole(strRole))
End Using
c#
var strRole = "ZooRole";
using (var context = new ApplicationDbContext())
{
var MyRoleStore = new RoleStore<IdentityRole>(context);
MyRoleStore.CreateAsync(new IdentityRole(strRole));
}
So, its rather easy to add some roles. Once of course you have the roles you want "defined" and added to the database, then you can/should be able to use the built in methods to add a new user to a role.
It is not clear if you issue/challenge here is adding/creating some new roles, of which THEN you can create users, and then add users to some defined roles.
So, trouble adding users? Trouble adding Roles? Trouble adding users to a given role? Which task?
In fact, during testing, I often have opened the above tables mannually, and simple edited the data by hand. (often, this becomes a chicken and egg issue. If I have a page setup to add/select users and assign roles, I can't get to that secuired page! (might have a role called "SiteAdmin". So, I often will just edit manaully my roles table (add a SiteAdmin), and add myselft to the "SiteAdmin" role.
it is actually quite easy to add some roles to a existing user manually as per above (ie: open the role table and add a few roles, and then open the AspNetUserRoles table and add myself to the SiteAdmin role (or whatever role you cooked up and floats your boat).