Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using Microsoft.AspNet.Identity;
- using Microsoft.AspNet.Identity.EntityFramework;
- using ProjekatRad.Models;
- namespace ProjekatRad.Logic
- {
- internal class Roles
- {
- 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 roleM = new RoleManager<IdentityRole>(roleStore);
- // Then, you create the "canEdit" role if it doesn't already exist.
- if (!roleM.RoleExists("Admin"))
- {
- IdRoleResult = roleM.Create(new IdentityRole { Name = "Admin" });
- }
- if (!roleM.RoleExists("Registrovan"))
- {
- IdRoleResult = roleM.Create(new IdentityRole { Name = "Registrovan" });
- }
- // 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 userM = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
- var appUser = new ApplicationUser
- {
- UserName = "canEditUser@wingtiptoys.com",
- Email = "canEditUser@wingtiptoys.com"
- };
- IdUserResult = userM.Create(appUser, "Pa$$word1");
- // If the new "canEdit" user was successfully created,
- // add the "canEdit" user to the "canEdit" role.
- if (!userM.IsInRole(userM.FindByEmail("canEditUser@wingtiptoys.com").Id, "Admin"))
- {
- IdUserResult = userM.AddToRole(userM.FindByEmail("canEditUser@wingtiptoys.com").Id, "Admin");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement