Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. [HttpPost]
  2. [ValidateAntiForgeryToken]
  3. public ActionResult Register(DataModels.RegisterViewModel model, HttpPostedFileBase files)
  4. {
  5. if (!ModelState.IsValid) return View(model);
  6.  
  7. string path = null;
  8. if (files != null && files.ContentLength > 0)
  9. {
  10. string filename = model.Login + ".jpg";
  11. path = Path.Combine(Server.MapPath("/App_Data/temp/Users"), filename);
  12. Random rnd = new Random(113);
  13. if (System.IO.File.Exists(path)) path = path + rnd.Next(99999);
  14. files.SaveAs(path);
  15. model.Photo = System.IO.File.ReadAllBytes(path);
  16. }
  17.  
  18. try { if (path != null) System.IO.File.Delete(path); }
  19. catch (IOException){ /* ignored*/ }
  20.  
  21. var user = CacheManager.CreateUser(model);
  22. if (user == null)
  23. {
  24. ModelState.AddModelError("", Resources.UserAlreadyExist);
  25. return View(model);
  26. }
  27.  
  28. IMailGunSender send = new MailGunSender();
  29. var body = "Для завершения регистрации перейдите по " +
  30. $"<a href="{Url.Action("ConfirmEmail", "Account", new {token = user.UserID.ToString(), user.Email}, Request.Url?.Scheme)}" title="Подтвердить регистрацию">{"ссылке"}</a>";
  31. try
  32. {
  33. send.SendHttp(Domain, Sender, Apikey, user.Email, "Подтверждение эл. почты MyBlog", body, true);
  34. return RedirectToAction("VerifyEmail", new { user.UserID});
  35. }
  36. catch (Exception)
  37. {
  38. send.SendSmtp(Smtplogin, Smtppass, user.Email, "Подтверждение эл. почты MyBlog", body, true);
  39. return RedirectToAction("VerifyEmail", new { user.UserID });
  40. }
  41. }
  42. public ActionResult VerifyEmail(int userid)
  43. {
  44. var user = CacheManager.GetUser(userid);
  45. ViewData.Model = user;
  46. return View();
  47. }
  48. [HttpPost]
  49. public ActionResult VerifyEmail(BlogUser user)
  50. {
  51. if (!ModelState.IsValid) View(user);
  52. if (user != null)
  53. {
  54. if (!SendVerificationEmail(user)) ModelState.AddModelError("", Resources.EmailSendingErr);
  55. else return RedirectToAction("VerifyEmail", new {user.UserID});
  56. }
  57. else ModelState.AddModelError("", Resources.UserNotExist);
  58. return View();
  59. }
  60.  
  61. [AllowAnonymous]
  62. public ActionResult ConfirmEmail(int token, string email)
  63. {
  64. var user = CacheManager.GetUser(Convert.ToInt32(token));
  65. if (user == null) return RedirectToAction("Register");
  66. if (user.Email != email) return RedirectToAction("VerifyEmail", new { user.UserID });
  67.  
  68. user.Verified = 1;
  69. CacheManager.SaveUser(user, false);
  70. return View("Login");
  71. }
  72.  
  73. public static BlogUser SaveUser(BlogUser user, bool hashPass)
  74. {
  75. return Rep.SaveUser(new BlogUser
  76. {
  77. Name = user.Name,
  78. Surname = user.Surname,
  79. Patronymic = user.Patronymic,
  80. UserLogin = user.UserLogin,
  81. Password = hashPass ? Hash(user.Password) : user.Password,
  82. Email = user.Email,
  83. UserPhoto = user.UserPhoto,
  84. UserID = user.UserID,
  85. RegistrationDate = user.RegistrationDate,
  86. UserRight = user.UserRight,
  87. Blocked = user.Blocked,
  88. Warnings = user.Warnings,
  89. Verified = user.Verified
  90. });
  91. }
  92.  
  93. public BlogUser SaveUser(BlogUser user)
  94. {
  95. if (user.UserID == 0) _db.BlogUser.Add(user);
  96. else
  97. {
  98. _db.BlogUser.Attach(_db.BlogUser.Single(u => u.UserID == user.UserID));
  99. ((IObjectContextAdapter) _db).ObjectContext.ApplyCurrentValues("BlogUser", user);
  100. }
  101. _db.SaveChanges();
  102. CacheManager.PurgeCacheItems("users");
  103. return GetUser(user.UserID);
  104. }
  105.  
  106. [AllowAnonymous]
  107. public ActionResult ConfirmEmail(int token, string email)
  108. {
  109. var user = CacheManager.GetUser(Convert.ToInt32(token));
  110. if (user == null) return RedirectToAction("Register");
  111. if (user.Email != email) return RedirectToAction("VerifyEmail", new { user.UserID });
  112.  
  113. CacheManager.SaveUser(new BlogUser
  114. {
  115. Blocked = 0,
  116. Email = user.Email,
  117. Password = user.Password,
  118. Verified = 1,
  119. UserID = user.UserID,
  120. Name = user.Name,
  121. Surname = user.Surname,
  122. Patronymic = user.Patronymic,
  123. UserRight = user.UserRight,
  124. Warnings = 0,
  125. UserLogin = user.UserLogin,
  126. UserPhoto = user.UserPhoto,
  127. RegistrationDate = user.RegistrationDate
  128. }, false);
  129. return View("Login");
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement