You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.4 KiB

  1. using Microsoft.AspNet.Identity;
  2. using Microsoft.AspNet.Identity.EntityFramework;
  3. using Microsoft.Owin.Security;
  4. using Sevomin.Models;
  5. using System;
  6. using System.Threading.Tasks;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. namespace Sevomin.WebFrontend.Controllers
  10. {
  11. public class AccountController : BaseController
  12. {
  13. public AccountController()
  14. : this(new UserManager<User>(new UserStore<User>(new UsersDbContext())))
  15. {
  16. }
  17. public AccountController(UserManager<User> userManager)
  18. {
  19. UserManager = userManager;
  20. UserManager.UserValidator = new Sevomin.Models.Helpers.SevominUserValidator();
  21. }
  22. public UserManager<User> UserManager { get; private set; }
  23. private IAuthenticationManager AuthenticationManager
  24. {
  25. get
  26. {
  27. return HttpContext.GetOwinContext().Authentication;
  28. }
  29. }
  30. [HttpPost]
  31. [ValidateAntiForgeryToken]
  32. public async Task<ActionResult> Signup(SignupViewModel model)
  33. {
  34. User user;
  35. if (model.IsAvalin)
  36. {
  37. user = new Avalin(model.Email, model.DisplayName);
  38. user.SignUpDate = DateTime.UtcNow;
  39. }
  40. else
  41. {
  42. int spaceIndex = model.DisplayName.IndexOf(' ');
  43. user = new Dovomin(model.Email, model.DisplayName, string.Empty);
  44. user.SignUpDate = DateTime.UtcNow;
  45. }
  46. var result = await UserManager.CreateAsync(user, model.Password);
  47. if (result.Succeeded)
  48. {
  49. await SignInAsync(user, isPersistent: false);
  50. return RedirectToAction("Index", "Home");
  51. }
  52. else
  53. {
  54. AddErrors(result);
  55. }
  56. return View("Intro", model);
  57. }
  58. public async Task<ActionResult> CheckUsername(string Email)
  59. {
  60. bool result = (await UserManager.FindByNameAsync(Email)) == null;
  61. if(result)
  62. return Json(true, JsonRequestBehavior.AllowGet);
  63. return Json("این ایمیل قبلا در سایت استفاده شده. کلمه عبور خود را فراموش کرده اید؟", JsonRequestBehavior.AllowGet);
  64. }
  65. public ActionResult Login(string returnUrl)
  66. {
  67. if(Request.IsAuthenticated)
  68. return RedirectToAction("Index", "Home");
  69. ViewBag.ReturnUrl = returnUrl;
  70. return View();
  71. }
  72. [HttpPost]
  73. [ValidateAntiForgeryToken]
  74. public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
  75. {
  76. if (ModelState.IsValid)
  77. {
  78. var user = await UserManager.FindAsync(model.Username, model.Password);
  79. if (user != null)
  80. {
  81. await SignInAsync(user, true);
  82. return RedirectToLocal(returnUrl);
  83. }
  84. else
  85. {
  86. ModelState.AddModelError("", "نام کاربری و یا کلمه عبور وارد شده صحیح نمی باشد.");
  87. }
  88. }
  89. // If we got this far, something failed, redisplay form
  90. return View(model);
  91. }
  92. public ActionResult Logout()
  93. {
  94. AuthenticationManager.SignOut();
  95. return RedirectToAction("Index", "Home");
  96. }
  97. private async Task SignInAsync(User user, bool isPersistent)
  98. {
  99. AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
  100. var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
  101. AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
  102. }
  103. private ActionResult RedirectToLocal(string returnUrl)
  104. {
  105. if (Url.IsLocalUrl(returnUrl))
  106. {
  107. return Redirect(returnUrl);
  108. }
  109. else
  110. {
  111. return RedirectToAction("Index", "Home");
  112. }
  113. }
  114. private void AddErrors(IdentityResult result)
  115. {
  116. foreach (var error in result.Errors)
  117. {
  118. ModelState.AddModelError("", error);
  119. }
  120. }
  121. }
  122. }