Advertisement
Guest User

Untitled

a guest
Feb 4th, 2018
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. //
  2. // POST: /Account/ForgotPassword
  3. [HttpPost]
  4. [AllowAnonymous]
  5. [ValidateAntiForgeryToken]
  6. public async Task<JsonResult> ForgotPassword(ForgotPasswordViewModel model, bool CaptchaValid)
  7. {
  8. string mensaje = String.Empty;
  9.  
  10. if (ModelState.IsValid)
  11. {
  12. if (!CaptchaValid)
  13. mensaje = "ERROR: Captcha inválido.";
  14. else
  15. {
  16. var user = await UserManager.FindByEmailAsync(model.Email);
  17. if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
  18. {
  19. mensaje = "ERROR: La dirección de correo electrónico no está registrada.";
  20. }
  21. else
  22. {
  23. var provider = new DpapiDataProtectionProvider("WebAttendance");
  24. UserManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser, string>(provider.Create("UserToken")) as IUserTokenProvider<ApplicationUser, string>;
  25. string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
  26. var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
  27. await UserManager.SendEmailAsync(user.Id, "Reset Password", "Por favor, cambie su contraseña al hacer click <a href="" + callbackUrl + "">aquí</a>");
  28. return Json("INFO: Se envió un mail a su cuenta de correo con instrucciones para cambiar su contraseña.");
  29. }
  30. }
  31. }
  32.  
  33. // If we got this far, something failed, redisplay form
  34. return Json(mensaje);
  35. }
  36.  
  37. <system.net>
  38. <mailSettings>
  39. <smtp from="info@xxx.com">
  40. <network host="mail.desytec.cl" password="xxxx" port="587" userName="yyy@xxx.cl" enableSsl="false"/>
  41. </smtp>
  42. </mailSettings>
  43. </system.net>
  44.  
  45. public class EmailService : IIdentityMessageService {
  46. public Task SendAsync(IdentityMessage message) {
  47. // Plug in your email service here to send an email.
  48. return Task.FromResult(0);
  49. }
  50. }
  51.  
  52. // code from https://stackoverflow.com/questions/40674457/how-to-configure-sender-email-credentials-for-asp-net-identity-usermanager-sende
  53. public async Task SendAsync(IdentityMessage message) {
  54. // Plug in your email service here to send an email.
  55. SmtpClient client = new SmtpClient();
  56. await client.SendMailAsync("email from web.config here",
  57. message.Destination,
  58. message.Subject,
  59. message.Body);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement