示例#1
0
        private List <SelectListItem> CreateRolesList()
        {
            var rolesList = new List <SelectListItem>();

            foreach (var role in ApplicationRolesDefinition.AllRoles())
            {
                rolesList.Add(new SelectListItem {
                    Text = role, Value = role
                });
            }

            return(rolesList);
        }
示例#2
0
        public async Task <AuthResultSM> SeedData()
        {
            try
            {
                foreach (var appRole in ApplicationRolesDefinition.AllRoles())
                {
                    var exists = await _roleManager.RoleExistsAsync(appRole);

                    if (!exists)
                    {
                        var role = (TAppRole)Activator.CreateInstance(typeof(TAppRole)); // TAppRole.Create();
                        role.Name = appRole;
                        await _roleManager.CreateAsync(role);
                    }
                }

                var adminUser = new UserSM
                {
                    Email     = "*****@*****.**",
                    FirstName = "Admin",
                    LastName  = "Admin"
                };

                // if there are any users in the admin role we don't want to create any users, so just return
                var usersInAdminRole = await _userManager.GetUsersInRoleAsync(ApplicationRolesDefinition.RoleAdmin);

                if (usersInAdminRole.Any())
                {
                    return(AuthResultFactory.CreateGenericSuccessResult());
                }

                var result = await RegisterUserAsync(adminUser, "Qwe!23");

                if (result.Succeeded)
                {
                    var user = await _userManager.FindByNameAsync(adminUser.Email);

                    await _userManager.AddToRoleAsync(user, ApplicationRolesDefinition.RoleAdmin);

                    return(AuthResultFactory.CreateGenericSuccessResult());
                }
                else
                {
                    return(AuthResultFactory.CreateGenericFailResult(result.Errors.FirstOrDefault()?.Description));
                }
            }
            catch (Exception ex)
            {
                return(AuthResultFactory.CreateGenericFailResult(ex.Message));
            }
        }