Advertisement
Guest User

Untitled

a guest
Dec 13th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. [HttpGet]
  2. public ActionResult LogIn()
  3. {
  4. return View();
  5. }
  6.  
  7. [HttpPost]
  8. public ActionResult LogIn(Models.User user)
  9. {
  10. if (IsValid(user.Email, user.Password))
  11. {
  12. FormsAuthentication.SetAuthCookie(user.Email, false);
  13. return RedirectToAction("Index", "Home");
  14. }
  15. else
  16. {
  17. ModelState.AddModelError("", "Login details are wrong.");
  18. }
  19. return View(user);
  20. }
  21.  
  22. [HttpGet]
  23. public ActionResult Register()
  24. {
  25. return View();
  26. }
  27.  
  28. [HttpPost]
  29. public ActionResult Register(Models.User user)
  30. {
  31. try
  32. {
  33.  
  34. if (ModelState.IsValid)
  35. {
  36. using (AppContext db = new AppContext())
  37. {
  38. var crypto = new SimpleCrypto.PBKDF2();
  39. var encrypPass = crypto.Compute(user.Password);
  40.  
  41. var newUser = db.Users.Create();
  42. newUser.FirstName = user.FirstName;
  43. newUser.LastName = user.LastName;
  44. newUser.Email = user.Email;
  45. newUser.CompanyName = user.CompanyName;
  46. newUser.Password = encrypPass;
  47. newUser.PasswordSalt = crypto.Salt;
  48. newUser.AdminCode = 0;
  49. user.Password = encrypPass;
  50. user.PasswordSalt = crypto.Salt;
  51.  
  52. db.Users.Add(newUser);
  53. db.SaveChanges();
  54. return RedirectToAction("Index", "Home");
  55.  
  56. }
  57. }
  58. else
  59. {
  60. ModelState.AddModelError("", "Data is not correct");
  61. }
  62. }
  63. catch (DbEntityValidationException e)
  64. {
  65. foreach (var validationErrors in e.EntityValidationErrors)
  66. {
  67. foreach (var validationError in validationErrors.ValidationErrors)
  68. {
  69. Trace.TraceInformation(
  70. "Class: {0}, Property: {1}, Error: {2}",
  71. validationErrors.Entry.Entity.GetType().FullName,
  72. validationError.PropertyName,
  73. validationError.ErrorMessage);
  74. }
  75. }
  76. }
  77. return View();
  78. }
  79.  
  80. private bool IsValid(string email, string password)
  81. {
  82. var crypto = new SimpleCrypto.PBKDF2();
  83. bool IsValid = false;
  84.  
  85. using (AppContext db = new AppContext())
  86. {
  87. var user = db.Users.FirstOrDefault(u => u.Email == email);
  88. if (user != null)
  89. {
  90. if (user.Password == crypto.Compute(user.PasswordSalt, password))
  91. {
  92. IsValid = true;
  93. }
  94. }
  95. }
  96. return IsValid;
  97. }
  98.  
  99. public ActionResult LogOut()
  100. {
  101. FormsAuthentication.SignOut();
  102. return RedirectToAction("Index", "Home");
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement