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.

117 lines
3.6 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. }
  21. public UserManager<User> UserManager { get; private set; }
  22. private IAuthenticationManager AuthenticationManager
  23. {
  24. get
  25. {
  26. return HttpContext.GetOwinContext().Authentication;
  27. }
  28. }
  29. [HttpPost]
  30. public async Task<ActionResult> Signup(SignupViewModel model)
  31. {
  32. User user;
  33. if (model.IsAvalin)
  34. {
  35. user = new Avalin(model.Email, model.DisplayName);
  36. user.SignUpDate = DateTime.UtcNow;
  37. }
  38. else
  39. {
  40. int spaceIndex = model.DisplayName.IndexOf(' ');
  41. user = new Dovomin(model.Email, model.DisplayName.Substring(0, spaceIndex), model.DisplayName.Substring(spaceIndex + 1));
  42. user.SignUpDate = DateTime.UtcNow;
  43. }
  44. var result = await UserManager.CreateAsync(user);
  45. if (result.Succeeded)
  46. {
  47. await SignInAsync(user, isPersistent: false);
  48. return RedirectToAction("Index", "Home");
  49. }
  50. else
  51. {
  52. AddErrors(result);
  53. }
  54. return View(model);
  55. }
  56. public ActionResult Login(string returnUrl)
  57. {
  58. ViewBag.ReturnUrl = returnUrl;
  59. return View();
  60. }
  61. [HttpPost]
  62. public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
  63. {
  64. if (ModelState.IsValid)
  65. {
  66. var user = await UserManager.FindAsync(model.Username, model.Password);
  67. if (user != null)
  68. {
  69. await SignInAsync(user, model.RememberMe);
  70. return RedirectToLocal(returnUrl);
  71. }
  72. else
  73. {
  74. ModelState.AddModelError("", "Invalid username or password.");
  75. }
  76. }
  77. // If we got this far, something failed, redisplay form
  78. return View(model);
  79. }
  80. private async Task SignInAsync(User user, bool isPersistent)
  81. {
  82. AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
  83. var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
  84. AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
  85. }
  86. private ActionResult RedirectToLocal(string returnUrl)
  87. {
  88. if (Url.IsLocalUrl(returnUrl))
  89. {
  90. return Redirect(returnUrl);
  91. }
  92. else
  93. {
  94. return RedirectToAction("Index", "Home");
  95. }
  96. }
  97. private void AddErrors(IdentityResult result)
  98. {
  99. foreach (var error in result.Errors)
  100. {
  101. ModelState.AddModelError("", error);
  102. }
  103. }
  104. }
  105. }