public async Task AddOrUpdate(UserEditInput userEditInput) { var userId = userEditInput.Id; var user = userId.HasValue ? await userManager.GetUserByIdAsync((long) userId) : new User(); await userManager.SetRoles(user, userEditInput.Roles); if (userEditInput.SetDefaultPassword) { userEditInput.Password = "******"; } else { if (string.IsNullOrWhiteSpace(userEditInput.Password)) { throw new UserFriendlyException("密码不能为空!"); } if (!userEditInput.Password.Equals(userEditInput.PasswordRepeat)) { throw new UserFriendlyException("2次密码不匹配!"); } } user.Name = userEditInput.Name; user.UserName = userEditInput.UserName; user.MobilePhone = userEditInput.MobilePhone; user.EmailAddress = userEditInput.EmailAddress; user.IsActive = userEditInput.IsActive; IdentityResult result; if (userEditInput.Id.HasValue) { result = await userManager.UpdateAsync(user); } else { result = await userManager.CreateAsync(user,userEditInput.Password); } if (userEditInput.SendActivationEmail) { // ReSharper disable once PossibleInvalidOperationException var emailConfirmToken = await userManager.GenerateEmailConfirmationTokenAsync((long)userId); string callbackUrl = "http://localhost:6234/Account/ConfirmEmail?userId="+userId+"&conformCode="+ HttpUtility.UrlEncode(emailConfirmToken); await userManager.SendEmailAsync((long)userId, "账号激活", "确定激活账号 <a href=\"" + callbackUrl + "\">确认</a>"); } if (!result.Succeeded) { throw new UserFriendlyException(result.Errors.FirstOrDefault()); } }
public async Task AddOrUpdate(UserEditInput userEditInput) { var userId = userEditInput.Id; var user = userId.HasValue ? await userManager.GetUserByIdAsync((long)userId) : new User(); await userManager.SetRoles(user, userEditInput.Roles); if (userEditInput.SetDefaultPassword) { userEditInput.Password = "******"; } else { if (string.IsNullOrWhiteSpace(userEditInput.Password)) { throw new UserFriendlyException("密码不能为空!"); } if (!userEditInput.Password.Equals(userEditInput.PasswordRepeat)) { throw new UserFriendlyException("2次密码不匹配!"); } } user.Name = userEditInput.Name; user.UserName = userEditInput.UserName; user.MobilePhone = userEditInput.MobilePhone; user.EmailAddress = userEditInput.EmailAddress; user.IsActive = userEditInput.IsActive; IdentityResult result; if (userEditInput.Id.HasValue) { result = await userManager.UpdateAsync(user); } else { result = await userManager.CreateAsync(user, userEditInput.Password); } if (userEditInput.SendActivationEmail) { // ReSharper disable once PossibleInvalidOperationException var emailConfirmToken = await userManager.GenerateEmailConfirmationTokenAsync((long)userId); string callbackUrl = "http://localhost:6234/Account/ConfirmEmail?userId=" + userId + "&conformCode=" + HttpUtility.UrlEncode(emailConfirmToken); await userManager.SendEmailAsync((long)userId, "账号激活", "确定激活账号 <a href=\"" + callbackUrl + "\">确认</a>"); } if (!result.Succeeded) { throw new UserFriendlyException(result.Errors.FirstOrDefault()); } }