Solved

Creating Roles in ASP.NET VS 2013

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

832 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