public async Task<IdentityResult> RegisterUserAsync(UserModel userModel) { var user = new ApplicationUser { UserName = userModel.UserName, DisplayName = userModel.DisplayName, Email = userModel.Email }; var identity = await userManager.CreateAsync(user, userModel.Password); if (!identity.Succeeded) { return identity; } foreach (var userRole in userModel.UserRoles.Split(',')) { if (roleManager.FindByName(userRole) == null) { await roleManager.CreateAsync(new ApplicationRole(userRole, userRole)); } await userManager.AddToRoleAsync(user.Id, userRole.Trim()); } return identity; }
public async Task<IHttpActionResult> Register(UserModel userModel) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var identityResult = await AuthenticationRepository.RegisterUserAsync(userModel); if (identityResult.Succeeded) { var user = await AuthenticationRepository.FindUserAsync(userModel.Email); var token = await AuthenticationRepository.GetEmailConfirmationCodeAsync(user.Id); var callbackLink = Url.Link("ConfirmEmail", new {userId = user.Id, code = token}); return Ok(callbackLink); } return BadRequest(identityResult.Errors.Aggregate(string.Empty, (current, error) => current + Environment.NewLine + error)); }