Hello,
I want to build an admin panel where the admin can create roles,users, assign users to roles and reset passwords. I'm using Identity 2.2.1 with web forms
I would appreciate any kind of help. Thank you in advance.
I' m having problem with the following two.
Resetting the password
I tried using the default ResetPassword.aspx in Account folder but I get the following error "An error has occurred" .
Create role and assign role at the same time:
I'm using the following code:
- protected void btnAssignRole_Click1(object sender, EventArgs e)
- {
- RoleActions role = new RoleActions();
-
- role.AddUserAndRole(roleTxt.Text, userTxt.Text, emailTxt.Text, passTxt.Text);
- }
but I get the following error "An exception of type 'System.InvalidOperationException' occurred in mscorlib.dll but was not handled in user code. Additional information: UserId not found.
" in RoleActions.cs - internal void AddUserAndRole(String role, String user, String email, String pass)
- {
- Models.ApplicationDbContext context = new ApplicationDbContext();
- IdentityResult IdRoleResult;
- IdentityResult IdUserResult;
-
- var roleStore = new RoleStore<IdentityRole>(context);
-
- var roleMgr = new RoleManager<IdentityRole>(roleStore);
-
- if (!roleMgr.RoleExists(role))
- {
- IdRoleResult = roleMgr.Create(new IdentityRole { Name = role });
- }
-
- var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
- var appUser = new ApplicationUser
- {
- UserName = user,
- Email = email
- };
- IdUserResult = userMgr.Create(appUser, pass);
-
- if (!userMgr.IsInRole(user, role))
- {
- IdUserResult = userMgr.AddToRole(user, role);
- }
- }