/// <summary> /// Create a user without or without a password and return a ClaimsIdentity representing the user created. /// </summary> /// <param name="request">Instance of CreateRequest</param> /// <returns>Instance of CreateResponse</returns> public async Task <CreateResponse> CreateAsync(CreateRequest request) { CreateResponse response = new CreateResponse(); try { AspNetUser newUser = new AspNetUser() { Id = Guid.NewGuid().ToString(), FirstName = request.FirstName, LastName = request.LastName, UserName = request.UserName, Discriminator = "ApplicationUser" //Change as per your requirements }; IdentityResult result; if (!string.IsNullOrWhiteSpace(request.Password)) { result = await this.UserManager.CreateAsync(newUser, request.Password); } else { result = await this.UserManager.CreateAsync(newUser); } if (result.Succeeded) { string authType = EnumStringValue.GetStringValue(request.AuthenticationType); ClaimsIdentity identity = await this.UserManager.CreateIdentityAsync(newUser, authType); response.UserId = new Guid(newUser.Id); response.ClaimIdentity = identity.ConvertToClaimIdentityView(); response.Success = true; } else { response.AddErrors(response.Errors); response.Success = false; } } catch (Exception e) { response.Success = false; response.Errors.Add(e.Message); } return(response); }
/// <summary> /// Create a user without or without a password and return a ClaimsIdentity representing the user created. /// </summary> /// <param name="request">Instance of CreateRequest</param> /// <returns>Instance of CreateResponse</returns> public async Task<CreateResponse> CreateAsync(CreateRequest request) { CreateResponse response = new CreateResponse(); try { AspNetUser newUser = new AspNetUser() { Id = Guid.NewGuid().ToString(), FirstName = request.FirstName, LastName = request.LastName, UserName = request.UserName, Discriminator = "ApplicationUser" //Change as per your requirements }; IdentityResult result; if (!string.IsNullOrWhiteSpace(request.Password)) result = await this.UserManager.CreateAsync(newUser, request.Password); else result = await this.UserManager.CreateAsync(newUser); if (result.Succeeded) { string authType = EnumStringValue.GetStringValue(request.AuthenticationType); ClaimsIdentity identity = await this.UserManager.CreateIdentityAsync(newUser, authType); response.UserId = new Guid(newUser.Id); response.ClaimIdentity = identity.ConvertToClaimIdentityView(); response.Success = true; } else { response.AddErrors(response.Errors); response.Success = false; } } catch (Exception e) { response.Success = false; response.Errors.Add(e.Message); } return response; }