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