Solved

Creating Roles in ASP.NET VS 2013

Posted on 2015-01-23
1
290 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

636 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