Answered by:
How to implement Roles in ASP.NET Identity 2.1 in Webforms

Question
-
User-78754308 posted
Hi,
I'm extremely frustrated and I could really use some help. I have an ASP.NET 4.5 webforms project that's using Identity version 2.1 - and I just want to add roles to it. I've searched and searched on it and read post after post on how to implement it, only to be lead down one rabbit hole after another. It seems next to impossible to find examples that aren't MVC. Every code example leaves me with new errors and new problems with the old ones remaining.
The last code example I tried leaves VS complaining with the following errors:
The type or namespace name 'RoleProvider' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'CustomRole' could not be found (are you missing a using directive or an assembly reference?)
I just want to get this to work. Isn't there a simple A-to-B-to-C-to-D tutorial out there to show how to implement this? This is so absolutely frustrating.
If anyone can help me with my problem I would really appreciate it.
<rant> New versions of things are supposed to make things more powerful and life easier, not make a developer have to spend countless hours fighting with a new authentication system, struggling with broken NuGet references, or reading a dozen tutorials for ASP.NET that turn out to be for MVC because Microsoft in their infinite wisdom introduced a different platform but gave it the same name as another platform. It really seems like ASP.NET Identity and NuGet weren't ready for primetime and were just thrown out, consequently throwing developers under the bus and making the ASP.NET platform a pain to develop and maintain - not to mention very expensive for any company using it since they have to pay their developers to try to hunt down solutions to these new problems that didn't exist in the old versions. </rant>
Wednesday, April 27, 2016 5:46 PM
Answers
-
User614698185 posted
Hi Piornet,
If you want to add a custom role and a user to the application, in the Global.asax.cs file, you could add below code:
// Create the custom role and user. RoleActions roleActions = new RoleActions(); roleActions.AddUserAndRole();
Then, create the class to add a custom role and a user:
using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.EntityFramework; internal class RoleActions { internal void AddUserAndRole() { // Access the application context and create result variables. Models.ApplicationDbContext context = new ApplicationDbContext(); IdentityResult IdRoleResult; IdentityResult IdUserResult; // Create a RoleStore object by using the ApplicationDbContext object. // The RoleStore is only allowed to contain IdentityRole objects. var roleStore = new RoleStore<IdentityRole>(context); // Create a RoleManager object that is only allowed to contain IdentityRole objects. // When creating the RoleManager object, you pass in (as a parameter) a new RoleStore object. var roleMgr = new RoleManager<IdentityRole>(roleStore); // Then, you create the "canEdit" role if it doesn't already exist. if (!roleMgr.RoleExists("canEdit")) { IdRoleResult = roleMgr.Create(new IdentityRole { Name = "canEdit" }); } // Create a UserManager object based on the UserStore object and the ApplicationDbContext // object. Note that you can create new objects and use them as parameters in // a single line of code, rather than using multiple lines of code, as you did // for the RoleManager object. var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context)); var appUser = new ApplicationUser { UserName = "canEditUser@wingtiptoys.com", Email = "canEditUser@wingtiptoys.com" }; IdUserResult = userMgr.Create(appUser, "Pa$$word1"); // If the new "canEdit" user was successfully created, // add the "canEdit" user to the "canEdit" role. if (!userMgr.IsInRole(userMgr.FindByEmail("canEditUser@wingtiptoys.com").Id, "canEdit")) { IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("canEditUser@wingtiptoys.com").Id, "canEdit"); } } }
Please refer to the following examples:
http://www.asp.net/web-forms/overview/older-versions-security/roles/creating-and-managing-roles-cs
Best Regards,
Candice Zhou
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, April 28, 2016 3:08 AM
All replies
-
User614698185 posted
Hi Piornet,
If you want to add a custom role and a user to the application, in the Global.asax.cs file, you could add below code:
// Create the custom role and user. RoleActions roleActions = new RoleActions(); roleActions.AddUserAndRole();
Then, create the class to add a custom role and a user:
using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.EntityFramework; internal class RoleActions { internal void AddUserAndRole() { // Access the application context and create result variables. Models.ApplicationDbContext context = new ApplicationDbContext(); IdentityResult IdRoleResult; IdentityResult IdUserResult; // Create a RoleStore object by using the ApplicationDbContext object. // The RoleStore is only allowed to contain IdentityRole objects. var roleStore = new RoleStore<IdentityRole>(context); // Create a RoleManager object that is only allowed to contain IdentityRole objects. // When creating the RoleManager object, you pass in (as a parameter) a new RoleStore object. var roleMgr = new RoleManager<IdentityRole>(roleStore); // Then, you create the "canEdit" role if it doesn't already exist. if (!roleMgr.RoleExists("canEdit")) { IdRoleResult = roleMgr.Create(new IdentityRole { Name = "canEdit" }); } // Create a UserManager object based on the UserStore object and the ApplicationDbContext // object. Note that you can create new objects and use them as parameters in // a single line of code, rather than using multiple lines of code, as you did // for the RoleManager object. var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context)); var appUser = new ApplicationUser { UserName = "canEditUser@wingtiptoys.com", Email = "canEditUser@wingtiptoys.com" }; IdUserResult = userMgr.Create(appUser, "Pa$$word1"); // If the new "canEdit" user was successfully created, // add the "canEdit" user to the "canEdit" role. if (!userMgr.IsInRole(userMgr.FindByEmail("canEditUser@wingtiptoys.com").Id, "canEdit")) { IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("canEditUser@wingtiptoys.com").Id, "canEdit"); } } }
Please refer to the following examples:
http://www.asp.net/web-forms/overview/older-versions-security/roles/creating-and-managing-roles-cs
Best Regards,
Candice Zhou
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, April 28, 2016 3:08 AM -
User-78754308 posted
Thank you so so much! Your answer solved my problem!
Thursday, April 28, 2016 7:18 PM