Beispiel #1
0
        private void CreateUser(ProblendDBContext context, string email, string password)
        {
            //create user manager
            var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context));

            //set user manager password
            userManager.PasswordValidator = new PasswordValidator
            {
                RequiredLength          = 4,
                RequireDigit            = false,
                RequireLowercase        = false,
                RequireNonLetterOrDigit = false,
                RequireUppercase        = false,
            };

            //create user object

            var admin = new ApplicationUser
            {
                UserName = email,
                Email    = email,
            };

            var result = userManager.Create(admin, password);

            if (!result.Succeeded)
            {
                throw new Exception(string.Join(";", result.Errors));
            }
        }
        private List <Role> GetUserRoles(ApplicationUser user, ProblendDBContext db)
        {
            var userManager = Request
                              .GetOwinContext()
                              .GetUserManager <ApplicationUserManager>();

            var roles = db.Roles
                        .Select(r => r.Name)
                        .OrderBy(r => r)
                        .ToList();

            var userRoles = new List <Role>();

            foreach (var roleName in roles)
            {
                var role = new Role {
                    Name = roleName
                };

                if (userManager.IsInRole(user.Id, roleName))
                {
                    role.IsSelected = true;
                }

                userRoles.Add(role);
            }

            return(userRoles);
        }
Beispiel #3
0
        private void CreateRole(ProblendDBContext context, string roleName)
        {
            var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));

            var result = roleManager.Create(new IdentityRole(roleName));

            if (!result.Succeeded)
            {
                throw new Exception(string.Join(";", result.Errors));
            }
        }
Beispiel #4
0
        private void SetRoleToUser(ProblendDBContext context, string email, string role)
        {
            var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context));

            var user   = context.Users.Where(u => u.Email == email).First();
            var result = userManager.AddToRole(user.Id, role);

            if (!result.Succeeded)
            {
                throw new Exception(string.Join(";", result.Errors));
            }
        }
        private HashSet <string> GetAdminUserNames(List <ApplicationUser> users, ProblendDBContext context)
        {
            var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context));

            var admins = new HashSet <string>();

            foreach (var user in users)
            {
                if (userManager.IsInRole(user.Id, "Admin"))
                {
                    admins.Add(user.UserName);
                }
            }

            return(admins);
        }
        private void SetUserRoles(EditUserViewModel model, ApplicationUser user, ProblendDBContext db)
        {
            var userManager = Request
                              .GetOwinContext()
                              .GetUserManager <ApplicationUserManager>();

            foreach (var role in model.Roles)
            {
                if (role.IsSelected)
                {
                    userManager.AddToRoles(user.Id, role.Name);
                }
                else if (!role.IsSelected)
                {
                    userManager.RemoveFromRole(user.Id, role.Name);
                }
            }
        }