Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. public class UserController : Controller
  2. {
  3. //
  4. // GET: /Register/
  5. public ActionResult Index()
  6. {
  7. return View();
  8. }
  9.  
  10. [HttpGet]
  11. public ActionResult LogIn()
  12. {
  13. return View();
  14. }
  15.  
  16. [HttpPost]
  17. public ActionResult LogIn(Models.Register userr)
  18. {
  19. if (IsValid(userr.Email_Id, userr.Password))
  20. {
  21. FormsAuthentication.SetAuthCookie(userr.Email_Id, false);
  22. return RedirectToAction("Index", "Home");
  23. }
  24. else
  25. {
  26. ModelState.AddModelError("", "Login details are wrong.");
  27. }
  28.  
  29. return View(userr);
  30. }
  31.  
  32. [HttpGet]
  33. public ActionResult Register()
  34. {
  35. return View();
  36. }
  37.  
  38. [HttpPost]
  39. public ActionResult Register(Models.Register user)
  40. {
  41. try
  42. {
  43. if (ModelState.IsValid)
  44. {
  45. using (var db = new MvcApplication2.Models.OnlineEducationEntities())
  46. {
  47.  
  48. var newUser = db.Registers.Create();
  49. newUser.Email_Id = user.Email_Id;
  50. newUser.Password = user.Password;
  51. newUser.Student_Name = user.Student_Name;
  52. newUser.DOB= DateTime.Now;
  53.  
  54. db.Registers.Add(newUser);
  55. db.SaveChanges();
  56. return RedirectToAction("LogIn", "User");
  57. }
  58. }
  59. else
  60. {
  61. ModelState.AddModelError("", "Data is not correct");
  62. }
  63. }
  64. catch (DbEntityValidationException e)
  65. {
  66. foreach (var eve in e.EntityValidationErrors)
  67. {
  68. Console.WriteLine("Entity of type "{0}" in state "{1}" has the following validation errors:",
  69. eve.Entry.Entity.GetType().Name, eve.Entry.State);
  70.  
  71. foreach (var ve in eve.ValidationErrors)
  72. {
  73. Console.WriteLine("- Property: "{0}", Error: "{1}"",
  74. ve.PropertyName, ve.ErrorMessage);
  75. }
  76. }
  77.  
  78. throw;
  79. }
  80.  
  81. return View();
  82. }
  83.  
  84. public ActionResult LogOut()
  85. {
  86. FormsAuthentication.SignOut();
  87. return RedirectToAction("LogIn", "User");
  88. }
  89.  
  90. private bool IsValid(string email, string password)
  91. {
  92. var crypto = new SimpleCrypto.PBKDF2();
  93. bool IsValid = false;
  94.  
  95. using (var db = new MvcApplication2.Models.OnlineEducationEntities())
  96. {
  97. var user = db.Registers.FirstOrDefault(u => u.Email_Id == email);
  98. if (user != null)
  99. {
  100. if (user.Password == crypto.Compute(password, user.Password))
  101. {
  102. IsValid = true;
  103. }
  104. }
  105. }
  106.  
  107. return IsValid;
  108. }
  109. }
  110.  
  111. @model MvcApplication2.Models.Register
  112. @{
  113. ViewBag.Title = "LogIn";
  114. Layout = "~/Views/Shared/_Layout.cshtml";
  115. }
  116.  
  117. <h2>LogIn</h2>
  118.  
  119. @using (Html.BeginForm()) {
  120. @Html.AntiForgeryToken()
  121. @Html.ValidationSummary(true)
  122.  
  123. <fieldset>
  124. <legend>Register</legend>
  125.  
  126. <div class="editor-label">
  127. @Html.LabelFor(model => model.Email_Id)
  128. </div>
  129. <div class="editor-field">
  130. @Html.EditorFor(model => model.Email_Id)
  131. @Html.ValidationMessageFor(model => model.Email_Id)
  132. </div>
  133.  
  134. <div class="editor-label">
  135. @Html.LabelFor(model => model.Password)
  136. </div>
  137. <div class="editor-field">
  138. @Html.EditorFor(model => model.Password)
  139. @Html.ValidationMessageFor(model => model.Password)
  140. </div>
  141.  
  142. <p>
  143. <input type="submit" value="LogIn" />
  144. </p>
  145. </fieldset>
  146. }
  147. < div>
  148. @Html.ActionLink("Register Now", "Register")
  149. </div>
  150.  
  151. @section Scripts {
  152. @Scripts.Render("~/bundles/jqueryval")
  153. }
  154.  
  155. private bool IsValid(string email, string password)
  156. {
  157. if (string.IsNullOrWhiteSpace(password) || password.Length < 6)
  158. return false;
  159.  
  160. //... etc.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement