Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. [AllowAnonymous, Route("confirm-email", Name = AccountControllerRoute.GetConfirmEmail)]
  2. public async Task<ActionResult> ConfirmEmail(int accountId, string token)
  3. {
  4. if (accountId == 0 || token == null)
  5. return HttpNotFound();
  6.  
  7. var account = await AccountManager.FindByIdAsync(accountId);
  8.  
  9. if (account == null)
  10. return HttpNotFound();
  11.  
  12. if (account.EmailConfirmed)
  13. return View("ConfirmEmail"); //return page that will say the token has expired
  14.  
  15. var result = await AccountManager.ConfirmEmailAsync(accountId, token);
  16.  
  17. var emailBody = string.Empty;
  18. if (result.Succeeded)
  19. {
  20. if(account.Institution == null)
  21. {
  22. // send message notifying admin that a new institution needs review and setup
  23. emailBody = $"{account.UserName}|{account.profile.contact}|{account.profile.institutionName}|{account.profile.institutionAddress}";
  24. await AccountManager.SendEmailAsync(account.Id, EmailNames.NewInstitutionRequest, "");
  25.  
  26. return View("ConfirmEmail");
  27. }
  28.  
  29. // Pass Name, Username
  30. emailBody = $"{account.Name}|{account.UserName}";
  31.  
  32. if (account.Institution.IsClient)
  33. {
  34. if (account.Institution.Licence.AllowedAccounts == 0)
  35. {
  36. await AccountManager.SendEmailAsync(account.Id, EmailNames.NewUserRequest, emailBody);
  37. return View("ConfirmEmail"); // send a different message notifying admin about exceeding allow accounts
  38. }
  39.  
  40. // send message notifying admin that a new user request for access
  41. await AccountManager.SendEmailAsync(account.Id, EmailNames.NewUserRequest, emailBody);
  42. return View("ConfirmEmail");
  43. }
  44. else
  45. {
  46. // send message notifying admin that a new user has registered
  47. await AccountManager.SendEmailAsync(account.Id, EmailNames.NewUserNotificationForSA, emailBody);
  48.  
  49. return View("ConfirmEmail");
  50. }
  51. }
  52.  
  53. return View("Error");
  54. }
  55.  
  56. public class PostalEmailService : IIdentityMessageService
  57. {
  58. public Task SendAsync(IdentityMessage message)
  59. {
  60. if (message == null)
  61. throw new ArgumentNullException("message");
  62.  
  63. EmailBase email;
  64. switch (message.Subject)
  65. {
  66. case EmailNames.ResetPassword:
  67. email = new ResetPasswordEmail {To = message.Destination, Link = message.Body};
  68. break;
  69.  
  70. case EmailNames.NewUserRequest:
  71. string name = message.Body.Split('|')[0];
  72. string userName = message.Body.Split('|')[1];
  73. email = new NewUserRequestEmail { To = "dummy@gmail.com", Name = name, UserName = userName };
  74. break;
  75. default:
  76. throw new InvalidOperationException("Unknown email '{0}'.".FormatWith(message.Subject));
  77. }
  78.  
  79. return email.SendAsync();
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement