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.

142 lines
4.5 KiB

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