Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Web.Security;
  7. using BookingAppStore.Models;
  8.  
  9. namespace BookingAppStore.Controllers
  10. {
  11. public class AccountController : Controller
  12. {
  13. UserContext dbU = new UserContext();
  14.  
  15. public int getRoleId()
  16. {
  17. int roleid = 0;
  18. foreach (var u in dbU.Users)
  19. {
  20. if (u.Email == User.Identity.Name)
  21. {
  22. roleid = u.RoleId;
  23. break;
  24. }
  25. }
  26. return roleid;
  27. }
  28.  
  29. public ActionResult Login()
  30. {
  31. ViewBag.Roleid = getRoleId();
  32. ViewBag.Username = User.Identity.Name;
  33.  
  34. return View();
  35. }
  36.  
  37. [HttpPost]
  38. [ValidateAntiForgeryToken]
  39. public ActionResult Login(LoginModel model)
  40. {
  41. if (ModelState.IsValid)
  42. {
  43. User user = null;
  44. using (UserContext db = new UserContext())
  45. {
  46. user = db.Users.FirstOrDefault(u => u.Email == model.Name && u.Password == model.Password);
  47. }
  48.  
  49. if (user != null)
  50. {
  51. FormsAuthentication.SetAuthCookie(model.Name, true);
  52. ViewBag.Roleid = getRoleId();
  53. ViewBag.Username = User.Identity.Name;
  54.  
  55. return RedirectToAction("Index", "Home");
  56. }
  57. else
  58. {
  59. ModelState.AddModelError("", "Такого пользователя не существует");
  60. }
  61. }
  62.  
  63. return View(model);
  64. }
  65.  
  66. public ActionResult Register()
  67. {
  68. ViewBag.Roleid = getRoleId();
  69. ViewBag.Username = User.Identity.Name;
  70.  
  71. return View();
  72. }
  73.  
  74. [HttpPost]
  75. [ValidateAntiForgeryToken]
  76. public ActionResult Register(RegisterModel model)
  77. {
  78. if (ModelState.IsValid)
  79. {
  80. User user = null;
  81. using (UserContext db = new UserContext())
  82. {
  83. user = db.Users.FirstOrDefault(u => u.Email == model.Name);
  84. }
  85.  
  86. if (user == null)
  87. {
  88. using (UserContext db = new UserContext())
  89. {
  90. db.Users.Add(new User { Email = model.Name, Password = model.Password, Age = model.Age, RoleId = 2 });
  91. db.SaveChanges();
  92.  
  93. user = db.Users.Where(u => u.Email == model.Name && u.Password == model.Password).FirstOrDefault();
  94. }
  95.  
  96. if (user != null)
  97. {
  98. FormsAuthentication.SetAuthCookie(model.Name, true);
  99. ViewBag.Roleid = getRoleId();
  100. ViewBag.Username = User.Identity.Name;
  101.  
  102. return RedirectToAction("Index", "Home");
  103. }
  104. }
  105. else
  106. {
  107. ModelState.AddModelError("", "Пользователь с таким логином уже существует");
  108. }
  109. }
  110.  
  111. return View(model);
  112. }
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement