コード例 #1
0
        private async Task ExecuteAsync()
        {
            if (await _userProvider.GetUserAsync(Username) == null)
            {
                var result = await _userProvider.CreateUserAsync(Username, Password, Email, null, null, true);

                if (result.Status == UIUserCreateStatus.Success)
                {
                    foreach (string role in Roles)
                    {
                        if (!await _roleProvider.RoleExistsAsync(role))
                        {
                            await _roleProvider.CreateRoleAsync(role);
                        }
                    }

                    await _roleProvider.AddUserToRolesAsync(result.User.Username, Roles);
                }
                else
                {
                    StringBuilder builder = new StringBuilder();

                    foreach (var error in result.Errors)
                    {
                        builder.AppendLine("* " + error);
                    }

                    throw new ArgumentException($"Failed creating user!\r\n{builder}");
                }
            }
        }
コード例 #2
0
        public async Task <ActionResult> Index(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await _userProvider.CreateUserAsync(model.Username, model.Password, model.Email, null, null, true);

                if (result.Status == UIUserCreateStatus.Success)
                {
                    await _roleProvider.CreateRoleAsync(AdminRoleName);

                    await _roleProvider.AddUserToRolesAsync(result.User.Username, new string[] { AdminRoleName });

                    AdministratorRegistrationPageMiddleware.IsEnabled = false;
                    SetFullAccessToWebAdmin();
                    var resFromSignIn = await _signInManager.SignInAsync(_userProvider.Name, model.Username, model.Password);

                    if (resFromSignIn)
                    {
                        return(Redirect("/"));
                    }
                }
                AddErrors(result.Errors);
            }
            // If we got this far, something failed, redisplay form
            return(View(model));
        }
コード例 #3
0
        private async void CreateUser(string username, string password, string email)
        {
            UIUserProvider userProvider = ServiceLocator.Current.GetInstance <UIUserProvider>();
            UIRoleProvider roleProvider = ServiceLocator.Current.GetInstance <UIRoleProvider>();

            if (await userProvider.GetUserAsync(username) == null)
            {
                var result = await userProvider.CreateUserAsync(username, password, email, null, null, true);

                if (result.Status == UIUserCreateStatus.Success)
                {
                    if (!await roleProvider.RoleExistsAsync(Roles.WebAdmins))
                    {
                        await roleProvider.CreateRoleAsync(Roles.WebAdmins);
                    }

                    await roleProvider.AddUserToRolesAsync(result.User.Username, new string[] { Roles.WebAdmins });
                }
            }
        }
コード例 #4
0
        private async Task CreateUser(string username, string email, IEnumerable <string> roles)
        {
            var result = await _uIUserProvider.CreateUserAsync(username, "Episerver123!", email, null, null, true);

            if (result.Status == UIUserCreateStatus.Success)
            {
                foreach (var role in roles)
                {
                    var exists = await _uIRoleProvider.RoleExistsAsync(role);

                    if (!exists)
                    {
                        await _uIRoleProvider.CreateRoleAsync(role);
                    }
                }

                await _uIRoleProvider.AddUserToRolesAsync(result.User.Username, roles);

                var resFromSignIn = await _uISignInManager.SignInAsync(_uIUserProvider.Name, username, "Episerver123!");
            }
        }