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)); }
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!"); } }