Guest User

gg

a guest
Aug 21st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.33 KB | None | 0 0
  1. using System;
  2. using System.Data.Entity;
  3. using System.Data.Entity.Validation;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Web.Mvc;
  7. using System.Web.Security;
  8. using WebApplication18.Models;
  9.  
  10. namespace WebApplication18.Controllers
  11. {
  12. public class UserController : Controller
  13. {
  14. private OnlineEducationEntities5 db = new OnlineEducationEntities5();
  15.  
  16. // GET: Registers
  17. public ActionResult Index()
  18. {
  19. return View(db.Registers.ToList());
  20. }
  21.  
  22. // GET: Registers/Details/5
  23. public ActionResult Details(int? id)
  24. {
  25. if (id == null)
  26. {
  27. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  28. }
  29. Register register = db.Registers.Find(id);
  30. if (register == null)
  31. {
  32. return HttpNotFound();
  33. }
  34. return View(register);
  35. }
  36.  
  37. // GET: Registers/Create
  38. public ActionResult Create()
  39. {
  40. return View();
  41. }
  42.  
  43. // POST: Registers/Create
  44. // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  45. // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
  46. [HttpPost]
  47. [ValidateAntiForgeryToken]
  48. public ActionResult Create([Bind(Include = "Student_ID,Student_Name,Email_Id,Password,DOB,Mobile_Number")] Register register)
  49. {
  50. if (ModelState.IsValid)
  51. {
  52. db.Registers.Add(register);
  53. db.SaveChanges();
  54. return RedirectToAction("Index");
  55. }
  56.  
  57. return View(register);
  58. }
  59.  
  60. // GET: Registers/Edit/5
  61. public ActionResult Edit(int? id)
  62. {
  63. if (id == null)
  64. {
  65. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  66. }
  67. Register register = db.Registers.Find(id);
  68. if (register == null)
  69. {
  70. return HttpNotFound();
  71. }
  72. return View(register);
  73. }
  74.  
  75. // POST: Registers/Edit/5
  76. // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  77. // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
  78. [HttpPost]
  79. [ValidateAntiForgeryToken]
  80. public ActionResult Edit([Bind(Include = "Student_ID,Student_Name,Email_Id,Password,DOB,Mobile_Number")] Register register)
  81. {
  82. if (ModelState.IsValid)
  83. {
  84. db.Entry(register).State = EntityState.Modified;
  85. db.SaveChanges();
  86. return RedirectToAction("Index");
  87. }
  88. return View(register);
  89. }
  90.  
  91. // GET: Registers/Delete/5
  92. public ActionResult Delete(int? id)
  93. {
  94. if (id == null)
  95. {
  96. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  97. }
  98. Register register = db.Registers.Find(id);
  99. if (register == null)
  100. {
  101. return HttpNotFound();
  102. }
  103. return View(register);
  104. }
  105.  
  106. // POST: Registers/Delete/5
  107. [HttpPost, ActionName("Delete")]
  108. [ValidateAntiForgeryToken]
  109. public ActionResult DeleteConfirmed(int id)
  110. {
  111. Register register = db.Registers.Find(id);
  112. db.Registers.Remove(register);
  113. db.SaveChanges();
  114. return RedirectToAction("Index");
  115. }
  116.  
  117. [HttpGet]
  118. public ActionResult LogIn()
  119. {
  120. return View();
  121. }
  122. public ActionResult StudentDisp(string email)
  123. {
  124.  
  125. return View();
  126.  
  127. }
  128.  
  129.  
  130.  
  131. //public ActionResult LogIn(string emailid, string password)
  132. //{
  133. // var cust = db.Registers.Where(x => x.Email_Id == emailid && x.Password == password);
  134. // if (cust == null)
  135. // {
  136. // ModelState.AddModelError("", "Login Failed");
  137. // return View();
  138. // }
  139. // else
  140. // {
  141. // Session["Email_Id"] = emailid;
  142. // return RedirectToAction("Index", "Home");
  143. // }
  144. //}
  145.  
  146.  
  147. [HttpPost]
  148. [ValidateAntiForgeryToken]
  149. public ActionResult LogIn(Models.Register userr)
  150. {
  151.  
  152.  
  153. if (userr.Email_Id == "admin@gmail.com" && userr.Password == "justdoit")
  154. {
  155. return RedirectToAction("Index", "Home");
  156. }
  157. else
  158. {
  159.  
  160.  
  161. if (IsValid(userr.Email_Id, userr.Password))
  162. {
  163.  
  164.  
  165. Session["Student_Name"] = db.Registers.FirstOrDefault().Student_Name;
  166. //FormsAuthentication.SetAuthCookie(userr.Email_Id, false);
  167. Session["Email_Id"] = userr.Email_Id;
  168. return RedirectToAction("Index", "Home");
  169. }
  170.  
  171.  
  172. else
  173. {
  174. //Session["Email_Id"] = db.Registers.SingleOrDefault().Email_Id;
  175. ModelState.AddModelError("", "Login details are wrong.");
  176. }
  177. return View(userr);
  178. }
  179. }
  180.  
  181. [HttpGet]
  182. public ActionResult Register()
  183. {
  184. return View();
  185. }
  186.  
  187. [HttpPost]
  188. public ActionResult Register(Models.Register user)
  189. {
  190. try
  191. {
  192. if (ModelState.IsValid)
  193. {
  194. using (var db = new WebApplication18.Models.OnlineEducationEntities5())
  195. {
  196.  
  197. var newUser = db.Registers.Create();
  198. newUser.Email_Id = user.Email_Id;
  199. newUser.Password = user.Password;
  200. newUser.Student_Name = user.Student_Name;
  201. newUser.DOB = user.DOB;
  202. newUser.Mobile_Number = user.Mobile_Number;
  203.  
  204. db.Registers.Add(newUser);
  205. db.SaveChanges();
  206. return RedirectToAction("LogIn", "User");
  207. }
  208. }
  209. else
  210. {
  211. ModelState.AddModelError("", "Data is not correct");
  212. }
  213. }
  214. catch (DbEntityValidationException e)
  215. {
  216. foreach (var eve in e.EntityValidationErrors)
  217. {
  218. Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
  219. eve.Entry.Entity.GetType().Name, eve.Entry.State);
  220. foreach (var ve in eve.ValidationErrors)
  221. {
  222. Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
  223. ve.PropertyName, ve.ErrorMessage);
  224. }
  225. }
  226. throw;
  227. }
  228. return View();
  229. }
  230.  
  231. public ActionResult LogOut()
  232. {
  233. FormsAuthentication.SignOut();
  234. return RedirectToAction("LogIn", "User");
  235. }
  236.  
  237. private bool IsValid(string email, string password)
  238. {
  239.  
  240. bool IsValid = false;
  241.  
  242. using (var db = new WebApplication18.Models.OnlineEducationEntities5())
  243. {
  244.  
  245. var user = db.Registers.FirstOrDefault(u => u.Email_Id == email);
  246. var user2 = db.Registers.FirstOrDefault(u => u.Password == password);
  247. if (user != null && user2 != null)
  248. {
  249.  
  250. //Console.WriteLine(password);
  251. //if (string.IsNullOrWhiteSpace(password) || password.Length < 6)
  252. // return false;
  253. //if (password == user.Password)
  254. //{
  255.  
  256. IsValid = true;
  257.  
  258.  
  259.  
  260. }
  261. return IsValid;
  262.  
  263. }
  264.  
  265.  
  266. }
  267.  
  268.  
  269.  
  270. protected override void Dispose(bool disposing)
  271. {
  272. if (disposing)
  273. {
  274. db.Dispose();
  275. }
  276. base.Dispose(disposing);
  277. }
  278. }
  279. }
Add Comment
Please, Sign In to add comment