public async Task <JsonResult> ResetPasswordGenerateToken(string email) { AppUser user = await userManager.FindByEmailAsync(email); if (user != null) { string token = await userManager.GeneratePasswordResetTokenAsync(user); string link = $@"{this.Request.Scheme}://{this.Request.Host}/#/resetPassword?userId={user.Id}&token={token}"; MailMessage mes = new MailMessage("*****@*****.**", "*****@*****.**", "aktywuj", link); MyEmailClient myEmailClient = new MyEmailClient(); await myEmailClient.SendEmail(mes); return(Json(new { Success = true })); } return(Json(new { Success = false, Errors = new List <string> { $"Użytkownik o adresie e-mail: {email} nie istnieje." } })); }
public async Task <JsonResult> Register([FromBody] CreateModel model) { if (ModelState.IsValid && model.Password == model.PasswordRepeated) { AppUser newUser = new AppUser { Email = model.Email, UserName = model.UserName }; IdentityResult result = await userManager.CreateAsync(newUser, model.Password); if (result.Succeeded) { string confirmationToken = userManager .GenerateEmailConfirmationTokenAsync(newUser).Result; string link = $@"{this.Request.Scheme}://{this.Request.Host}/#/confirmEmail?userId={newUser.Id}&token={confirmationToken}"; MailMessage mes = new MailMessage("*****@*****.**", "*****@*****.**", "aktywuj", link); MyEmailClient myEmailClient = new MyEmailClient(); await myEmailClient.SendEmail(mes); return(Json(new { Success = true, Message = "Grtulacje!" })); } // identityResult == false List <string> errors = new List <string>(); foreach (var err in result.Errors) { errors.Add(err.Description); } return(Json(new { Success = false, Errors = errors })); } return(Json(new { Success = false, Errors = new List <string> { "Wprowadzone dane nie spełniają wymagań aplikacji. Popraw je i spróbuj jeszcze raz." } })); }
public async Task <JsonResult> UpdateUser([FromBody] UpdateUserModel model) { AppUser user = await userManager.GetUserAsync(HttpContext.User); if (user != null) { if (userManager.PasswordHasher.VerifyHashedPassword(user, user.PasswordHash, model.Password) == PasswordVerificationResult.Success) { //jesli nowa nazwa jest inna - zmienia IdentityResult updateUserNameResult = null; if (user.UserName != model.UserName) { updateUserNameResult = await userManager.SetUserNameAsync(user, model.UserName); if (!updateUserNameResult.Succeeded) { List <string> errors = new List <string>(); foreach (var err in updateUserNameResult.Errors) { errors.Add(err.Description); } return(Json(new { Success = false, Errors = errors })); } await signInManager.RefreshSignInAsync(user); } // jesli nowy email jest inny - zmienia if (model.Email != user.Email) { string token = await userManager.GenerateChangeEmailTokenAsync(user, model.Email); string link = Url.Action("ChangeEmail", "User", new { userId = user.Id, token, email = model.Email }, protocol: HttpContext.Request.Scheme); MailMessage mes = new MailMessage("*****@*****.**", "*****@*****.**", "aktywuj", link); MyEmailClient myEmailClient = new MyEmailClient(); await myEmailClient.SendEmail(mes); } if (updateUserNameResult != null && model.Email != user.Email) { return(Json(new { Success = true, UserNameChanged = true, user.UserName, EmailChanged = true })); } else if (updateUserNameResult != null && model.Email == user.Email) { return(Json(new { Success = true, UserNameChanged = true, user.UserName, EmailChanged = false })); } else if (updateUserNameResult == null && model.Email != user.Email) { return(Json(new { Success = true, UserNameChanged = false, EmailChanged = true })); } } return(Json(new { Success = false, Errors = new List <string> { "Wprowadzone hasło jest nieprawidłowe!" } })); } return(Json(new { Success = false, Errors = new List <string> { "Odśwież stronę i zaloguj się ponownie!" } })); }