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