Guest User

Untitled

a guest
Nov 1st, 2017
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. [Authorize]
  2. public class AccountController : Controller
  3. {
  4. private UserService _userService;
  5. public UserService UserService{
  6. get { return _userService ?? (_userService = new UserService()); }
  7. }
  8.  
  9. [HttpPost]
  10. [AllowAnonymous]
  11. [ValidateAntiForgeryToken]
  12. public async Task<ActionResult> Login(LoginViewModel model, string returnUrl){
  13. if (!ModelState.IsValid)
  14. {
  15. return View(model);
  16. }
  17.  
  18. //the line with SignInManager is Default in project
  19. //var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
  20.  
  21. //I have implemented my User service which checks in DB is there exists such a user with email and password and returns the same SignInStatus
  22. var result = UserService.Authenticate(model.Email, model.Password);
  23.  
  24. switch (result)
  25. {
  26. case SignInStatus.Success:
  27. return RedirectToLocal(returnUrl);
  28. case SignInStatus.LockedOut:
  29. return View("Lockout");
  30. case SignInStatus.RequiresVerification:
  31. return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
  32. case SignInStatus.Failure:
  33. default:
  34. ModelState.AddModelError("", "Invalid login attempt.");
  35. return View(model);
  36. }
  37. }
  38. }
  39.  
  40. public class UserService : IUserService
  41. {
  42. public SignInStatus Authenticate(string email, string password)
  43. {
  44. if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
  45. {
  46. return SignInStatus.Failure;
  47. }
  48.  
  49. //TODO: perform authentication against DB account
  50. if (email == "mymail@mail.com" && password == "123")
  51. {
  52. return SignInStatus.Success;
  53. }
  54. else
  55. {
  56. return SignInStatus.Failure;
  57. }
  58. }
  59. }
  60.  
  61. public class AdministrationController : Controller
  62. {
  63. // GET: Admin/Admin
  64. [Authorize]
  65. public ActionResult Index()
  66. {
  67. return View();
  68. }
  69. }
  70.  
  71. public class AdministrationController : Controller
  72. {
  73. // GET: Admin/Admin
  74. [Authorize(Roles = "Administrator")]
  75. public ActionResult Index()
  76. {
  77. return View();
  78. }
  79. }
  80.  
  81. case SignInStatus.Success:
  82. var user = new ApplicationUser
  83. {
  84. Email = model.Email,
  85. UserName = model.Email,
  86. ... set any other properties that you find convenient
  87. };
  88. await SignInManager.SignInAsync(user, false, false);
  89. return RedirectToLocal(returnUrl);
Add Comment
Please, Sign In to add comment