Guest User

Untitled

a guest
Mar 27th, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. [Authorize]
  2. [Route("confirm", Name = "ConfirmEmail")]
  3. public async Task<ActionResult> ConfirmEmail()
  4. {
  5. var user = await UserManager.FindByNameAsync(User.Identity.Name);
  6.  
  7. if (user != null)
  8. {
  9. var provider = new DpapiDataProtectionProvider("InternetShop");
  10. UserManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(provider.Create("EmailConfirmation"));
  11.  
  12. string token = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
  13. string callBack = Url.RouteUrl("ConfirmEmail", new { userId = user.Id, token },
  14. protocol: Request.Url.Scheme);
  15.  
  16. await UserManager.SendEmailAsync(user.Id, "Confirm email", $"For a registration competion" +
  17. $"please folow the link: {callBack}");
  18.  
  19. ViewBag.email = user.Email;
  20.  
  21. return View("SendConfirmationCode");
  22. }
  23. else return View("Error");
  24. }
  25.  
  26. public class EmailService : IIdentityMessageService
  27. {
  28. public Task SendAsync(IdentityMessage message)
  29. {
  30. var from = ConfigurationManager.AppSettings["Email"];
  31. var pass = ConfigurationManager.AppSettings["Password"];
  32.  
  33. SmtpClient client = new SmtpClient("smtp.yandex.ru", 25);
  34.  
  35. client.DeliveryMethod = SmtpDeliveryMethod.Network;
  36. client.UseDefaultCredentials = false;
  37. client.Credentials = new System.Net.NetworkCredential(from, pass);
  38. client.EnableSsl = true;
  39.  
  40. var mail = new MailMessage(from, message.Destination);
  41. mail.Subject = message.Subject;
  42. mail.Body = message.Body;
  43. mail.IsBodyHtml = true;
  44.  
  45. return client.SendMailAsync(mail);
  46. }
  47. }
Add Comment
Please, Sign In to add comment