// Update this to accept an argument of type ApplicationRole:
        public SelectRoleEditorViewModel(ApplicationRole role)
        {
            this.RoleName = role.Name;

            // Assign the new Descrption property:
            this.Description = role.Description;
        }
 public EditRoleViewModel(ApplicationRole role)
 {
     this.OriginalRoleName = role.Name;
     this.RoleName = role.Name;
     this.Description = role.Description;
 }
 public RoleViewModel(ApplicationRole role)
 {
     this.RoleName = role.Name;
     this.Description = role.Description;
 }
Example #4
0
        //Create [email protected] with password=Admin@123456 in the Admin role
        public static void InitializeIdentityForEF(ApplicationDbContext db)
        {
            var    userManager = HttpContext.Current.GetOwinContext().GetUserManager <ApplicationUserManager>();
            var    roleManager = HttpContext.Current.GetOwinContext().Get <ApplicationRoleManager>();
            string name        = "*****@*****.**";
            string password    = "******";
            string roleName    = "Administrator";

            //Create Role Admin if it does not exist
            var role = roleManager.FindByName(roleName);

            if (role == null)
            {
                // *** INITIALIZE WITH CUSTOM APPLICATION ROLE CLASS:
                role = new ApplicationRole(roleName);
                var roleresult = roleManager.Create(role);
            }

            var user = userManager.FindByName(name);

            if (user == null)
            {
                user = new ApplicationUser {
                    UserName = name, Email = name
                };
                var result = userManager.Create(user, password);
                result = userManager.SetLockoutEnabled(user.Id, false);
            }

            // Add user admin to Role Admin if not already added
            var rolesForUser = userManager.GetRoles(user.Id);

            if (!rolesForUser.Contains(role.Name))
            {
                var result = userManager.AddToRole(user.Id, role.Name);
            }

            //Create User1
            name     = "*****@*****.**";
            password = "******";
            roleName = "Employee";

            //Create Role Admin if it does not exist
            role = roleManager.FindByName(roleName);
            if (role == null)
            {
                // *** INITIALIZE WITH CUSTOM APPLICATION ROLE CLASS:
                role = new ApplicationRole(roleName);
                var roleresult = roleManager.Create(role);
            }

            user = userManager.FindByName(name);
            if (user == null)
            {
                user = new ApplicationUser {
                    UserName = name, Email = name
                };
                var result = userManager.Create(user, password);
                result = userManager.SetLockoutEnabled(user.Id, false);
            }

            // Add user admin to Role Admin if not already added
            rolesForUser = userManager.GetRoles(user.Id);
            if (!rolesForUser.Contains(role.Name))
            {
                var result = userManager.AddToRole(user.Id, role.Name);
            }

            //Create User2
            name     = "*****@*****.**";
            password = "******";
            roleName = "Employee";

            //Create Role Admin if it does not exist
            role = roleManager.FindByName(roleName);
            if (role == null)
            {
                // *** INITIALIZE WITH CUSTOM APPLICATION ROLE CLASS:
                role = new ApplicationRole(roleName);
                var roleresult = roleManager.Create(role);
            }

            user = userManager.FindByName(name);
            if (user == null)
            {
                user = new ApplicationUser {
                    UserName = name, Email = name
                };
                var result = userManager.Create(user, password);
                result = userManager.SetLockoutEnabled(user.Id, false);
            }

            // Add user admin to Role Admin if not already added
            rolesForUser = userManager.GetRoles(user.Id);
            if (!rolesForUser.Contains(role.Name))
            {
                var result = userManager.AddToRole(user.Id, role.Name);
            }
        }