public void PopulateRolesAndAdmin()
        {
            // Check to see if Role Exists, if not create it
            if (!_roleManager.RoleExists(SecurityConfig.AdminRole))
            {
                _logger.Info($"Role {SecurityConfig.AdminRole} does not exist - adding to Roles");
                var result = _roleManager.Create(new IdentityRole(SecurityConfig.AdminRole));
                if (result.Succeeded == false)
                {
                    throw new Exception($"RoleManager.Create (Admin) failed: {result.Errors.JoinByNewline()}");
                }
            }

            if (!_roleManager.RoleExists(SecurityConfig.UserRole))
            {
                _logger.Info($"Role {SecurityConfig.UserRole} does not exist - adding to Roles");
                var result = _roleManager.Create(new IdentityRole(SecurityConfig.UserRole));
                if (result.Succeeded == false)
                {
                    throw new Exception($"RoleManager.Create (User) failed: {result.Errors.JoinByNewline()}");
                }
            }

            var adminUser = _userManager.FindByName(SecurityConfig.DefaultAdminEmail);

            if (adminUser == null)
            {
                using (var transaction = _dbContext.Database.BeginTransaction())
                {
                    _logger.Info(
                        $"Unable to locate default Sys Admin: {SecurityConfig.DefaultAdminEmail} - "
                        + @"creating new Sys Admin");

                    var newAdminUser = new ApplicationUser()
                    {
                        UserName = SecurityConfig.DefaultAdminEmail,
                        Email    = SecurityConfig.DefaultAdminEmail,
                    };

                    var result = _userManager.Create(newAdminUser, SecurityConfig.DefaultAdminPassword);
                    if (result.Succeeded == false)
                    {
                        throw new Exception(
                                  $"UserManager.Create failed: {result.Errors.JoinByNewline()}");
                    }

                    var resultAddToAdmin = _userManager.AddToRole(newAdminUser.Id, SecurityConfig.AdminRole);
                    if (resultAddToAdmin.Succeeded == false)
                    {
                        throw new Exception(
                                  $"UserManager.AddToRole (Admin) failed: {resultAddToAdmin.Errors.JoinByNewline()}");
                    }

                    transaction.Commit();
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates default roles.
        /// </summary>
        public void CreateRoles(DataContext context)
        {
            var roleManager = new IdentityRoleManager(new RoleStore(context));

            if (!roleManager.RoleExists(RoleNames.Admin))
            {
                var result = roleManager.Create(new Role(RoleNames.Admin));

                if (!result.Succeeded)
                {
                    throw new Exception("Error creating admin role: " + result.Errors.FirstOrDefault());
                }
            }

            if (!roleManager.RoleExists(RoleNames.User))
            {
                var result = roleManager.Create(new Role(RoleNames.User));

                if (!result.Succeeded)
                {
                    throw new Exception("Error creating user role: " + result.Errors.FirstOrDefault());
                }
            }
        }
Esempio n. 3
0
 public bool RoleExists(string roleName)
 {
     return(_roleManager.RoleExists(roleName));
 }
Esempio n. 4
0
 public bool RoleExists(IdentityRoleManager roleManager, string name)
 {
     return roleManager.RoleExists(name);
 }