Solved

Creating Roles in ASP.NET VS 2013

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Crystal Report for VS2013 17 42
getting id from database 5 24
Closing all open child forms. 4 24
Problem to page 4 27
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now