public async Task<UserManagementResult<string>> CreateUserAsync(string username, string password, IEnumerable<Claim> claims) { try { //Check the email address is unique if(manager.CheckEmailAlreadyInUse(claims)) { throw new Exception("This email is already in use by an existing user");//This will be turned into the UserManagementResult with the errors array } var name = !string.IsNullOrEmpty(username) ? username : Guid.NewGuid().ToString("N"); var user = new User() { UserName = name, DisplayName = name, }; //These seems to have to be done before the create, otherwise doesn't save (and I don't have access to context save?) //Ensure that we don't accidentally pick up the password claim! var SafeClaims = claims.Where(c => c.Type != Core.Constants.ClaimTypes.Password && c.Type != Core.Constants.ClaimTypes.Name); var RemainingClaims = SafeClaims.Where(c => c.Type != Core.Constants.ClaimTypes.Email && c.Type != Core.Constants.ClaimTypes.Phone); foreach (var claim in RemainingClaims) { TypeUtilities.SetProperty(user, claim.Type, claim.Value, true); } //Create a user with the supplied password var result = await manager.CreateAsync(user, password); if(result.Errors.Any()) { return new UserManagementResult<string>(result.Errors); } //Deal with the claims if(manager.SupportsUserEmail) { claims = await manager.SetAccountEmailAsync(user.Id, claims); } if(manager.SupportsUserPhoneNumber) { claims = await manager.SetAccountPhoneAsync(user.Id, claims); } if (manager.SupportsUserClaim) { claims = await manager.SetOtherClaims(user.Id, claims); } if (manager.SupportsUserRole) { claims = await manager.SetRolesForUser(user.Id, claims); } return new UserManagementResult<string>(user.Id); } catch (Exception e) { return new UserManagementResult<string>(new[] { e.Message }); } }
protected virtual Task<User> InstantiateNewUserFromExternalProviderAsync(string provider, string providerId, IEnumerable<Claim> claims) { var user = new User() { UserName = Guid.NewGuid().ToString("N") }; return Task.FromResult(user); }
protected virtual async Task<IEnumerable<Claim>> GetClaimsForAuthenticateResult(User user) { List<Claim> claims = new List<Claim>(); if (EnableSecurityStamp && manager.SupportsUserSecurityStamp) { var stamp = await manager.GetSecurityStampAsync(user.Id); if (!String.IsNullOrWhiteSpace(stamp)) { claims.Add(new Claim("security_stamp", stamp)); } } return claims; }
protected virtual async Task<IEnumerable<Claim>> GetClaimsFromAccount(User user) { var claims = new List<Claim> { new Claim(Core.Constants.ClaimTypes.Subject, user.Id.ToString()), new Claim(Core.Constants.ClaimTypes.Name, user.UserName), new Claim(Core.Constants.ClaimTypes.PreferredUserName, user.UserName), }; if (manager.SupportsUserEmail) { var email = await manager.GetEmailAsync(user.Id); if (!String.IsNullOrWhiteSpace(email)) { claims.Add(new Claim(OpenIDConnect.Core.Constants.ClaimTypes.Email, email)); var verified = await manager.IsEmailConfirmedAsync(user.Id); claims.Add(new Claim(OpenIDConnect.Core.Constants.ClaimTypes.EmailVerified, verified ? "true" : "false")); } } if (manager.SupportsUserPhoneNumber) { var phone = await manager.GetPhoneNumberAsync(user.Id); if (!String.IsNullOrWhiteSpace(phone)) { claims.Add(new Claim(OpenIDConnect.Core.Constants.ClaimTypes.PhoneNumber, phone)); var verified = await manager.IsPhoneNumberConfirmedAsync(user.Id); claims.Add(new Claim(OpenIDConnect.Core.Constants.ClaimTypes.PhoneNumberVerified, verified ? "true" : "false")); } } if (manager.SupportsUserClaim) { claims.AddRange(await manager.GetClaimsAsync(user.Id)); } if (manager.SupportsUserRole) { var roleClaims = from role in await manager.GetRolesAsync(user.Id) select new Claim(OpenIDConnect.Core.Constants.ClaimTypes.Role, role); claims.AddRange(roleClaims); } return claims; }
/// <summary> /// Doing nothing /// </summary> /// <param name="user"></param> /// <param name="signInData"></param> /// <returns></returns> protected virtual Task<AuthenticationResult> PostAuthenticateLocalAsync(User user, SignInData signInData) { if (user == null) { return Task.FromResult<AuthenticationResult>(null); } return Task.FromResult(new AuthenticationResult( user.Id, user.UserName, user.Claims.Select(c => new Claim(c.ClaimType, c.ClaimValue)))); }