Solved

Creating Roles in ASP.NET VS 2013

Posted on 2015-01-23
1
286 Views
Last Modified: 2015-01-26
I have just made the leap to VS 2013.  New to the Identity model.  Trouble finding some clear instructions on creating an 'Admin" role and assigning a user to that role.  I should add the web site is a webforms site.
0
Comment
Question by:rrhandle8
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 40570270
Assuming you are using the default role provider and context, you should be able to do something like:
// grab the context
ApplicationDbContext context = HttpContext.GetOwinContext().Get<ApplicationDbContext>();

// create a new user
ApplicationUser user = new ApplicationUser() { UserName = "TestUser", Email = "test@testmail.com" };
IdentityResult result = UserManager.Create(user);

// create a role manager from the context
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));

// check if the "Admin" role exists; if it doesn't, create it
IdentityRole role = roleManager.FindByName("Admin");
if (role == null)
{
    role = new IdentityRole("Admin");
    roleManager.Create(role);
}

// add the user to the role
UserManager.AddToRole(user.Id, role.Name);

Open in new window

0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Validation for a field in asp.net 3 31
CHANGE FORM TEXT BEFORE IT SHOW TO USER 13 33
async questions 5 49
Click print button programmatically on print showdialog 1 24
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question