Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Data.Entity;
- using System.Data.Entity.Validation;
- using System.Linq;
- using System.Net;
- using System.Web.Mvc;
- using System.Web.Security;
- using WebApplication18.Models;
- namespace WebApplication18.Controllers
- {
- public class UserController : Controller
- {
- private OnlineEducationEntities5 db = new OnlineEducationEntities5();
- // GET: Registers
- public ActionResult Index()
- {
- return View(db.Registers.ToList());
- }
- // GET: Registers/Details/5
- public ActionResult Details(int? id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- Register register = db.Registers.Find(id);
- if (register == null)
- {
- return HttpNotFound();
- }
- return View(register);
- }
- // GET: Registers/Create
- public ActionResult Create()
- {
- return View();
- }
- // POST: Registers/Create
- // To protect from overposting attacks, please enable the specific properties you want to bind to, for
- // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Create([Bind(Include = "Student_ID,Student_Name,Email_Id,Password,DOB,Mobile_Number")] Register register)
- {
- if (ModelState.IsValid)
- {
- db.Registers.Add(register);
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- return View(register);
- }
- // GET: Registers/Edit/5
- public ActionResult Edit(int? id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- Register register = db.Registers.Find(id);
- if (register == null)
- {
- return HttpNotFound();
- }
- return View(register);
- }
- // POST: Registers/Edit/5
- // To protect from overposting attacks, please enable the specific properties you want to bind to, for
- // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Edit([Bind(Include = "Student_ID,Student_Name,Email_Id,Password,DOB,Mobile_Number")] Register register)
- {
- if (ModelState.IsValid)
- {
- db.Entry(register).State = EntityState.Modified;
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- return View(register);
- }
- // GET: Registers/Delete/5
- public ActionResult Delete(int? id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- Register register = db.Registers.Find(id);
- if (register == null)
- {
- return HttpNotFound();
- }
- return View(register);
- }
- // POST: Registers/Delete/5
- [HttpPost, ActionName("Delete")]
- [ValidateAntiForgeryToken]
- public ActionResult DeleteConfirmed(int id)
- {
- Register register = db.Registers.Find(id);
- db.Registers.Remove(register);
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- [HttpGet]
- public ActionResult LogIn()
- {
- return View();
- }
- public ActionResult StudentDisp(string email)
- {
- return View();
- }
- //public ActionResult LogIn(string emailid, string password)
- //{
- // var cust = db.Registers.Where(x => x.Email_Id == emailid && x.Password == password);
- // if (cust == null)
- // {
- // ModelState.AddModelError("", "Login Failed");
- // return View();
- // }
- // else
- // {
- // Session["Email_Id"] = emailid;
- // return RedirectToAction("Index", "Home");
- // }
- //}
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult LogIn(Models.Register userr)
- {
- if (userr.Email_Id == "admin@gmail.com" && userr.Password == "justdoit")
- {
- return RedirectToAction("Index", "Home");
- }
- else
- {
- if (IsValid(userr.Email_Id, userr.Password))
- {
- Session["Student_Name"] = db.Registers.FirstOrDefault().Student_Name;
- //FormsAuthentication.SetAuthCookie(userr.Email_Id, false);
- Session["Email_Id"] = userr.Email_Id;
- return RedirectToAction("Index", "Home");
- }
- else
- {
- //Session["Email_Id"] = db.Registers.SingleOrDefault().Email_Id;
- ModelState.AddModelError("", "Login details are wrong.");
- }
- return View(userr);
- }
- }
- [HttpGet]
- public ActionResult Register()
- {
- return View();
- }
- [HttpPost]
- public ActionResult Register(Models.Register user)
- {
- try
- {
- if (ModelState.IsValid)
- {
- using (var db = new WebApplication18.Models.OnlineEducationEntities5())
- {
- var newUser = db.Registers.Create();
- newUser.Email_Id = user.Email_Id;
- newUser.Password = user.Password;
- newUser.Student_Name = user.Student_Name;
- newUser.DOB = user.DOB;
- newUser.Mobile_Number = user.Mobile_Number;
- db.Registers.Add(newUser);
- db.SaveChanges();
- return RedirectToAction("LogIn", "User");
- }
- }
- else
- {
- ModelState.AddModelError("", "Data is not correct");
- }
- }
- catch (DbEntityValidationException e)
- {
- foreach (var eve in e.EntityValidationErrors)
- {
- Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
- eve.Entry.Entity.GetType().Name, eve.Entry.State);
- foreach (var ve in eve.ValidationErrors)
- {
- Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
- ve.PropertyName, ve.ErrorMessage);
- }
- }
- throw;
- }
- return View();
- }
- public ActionResult LogOut()
- {
- FormsAuthentication.SignOut();
- return RedirectToAction("LogIn", "User");
- }
- private bool IsValid(string email, string password)
- {
- bool IsValid = false;
- using (var db = new WebApplication18.Models.OnlineEducationEntities5())
- {
- var user = db.Registers.FirstOrDefault(u => u.Email_Id == email);
- var user2 = db.Registers.FirstOrDefault(u => u.Password == password);
- if (user != null && user2 != null)
- {
- //Console.WriteLine(password);
- //if (string.IsNullOrWhiteSpace(password) || password.Length < 6)
- // return false;
- //if (password == user.Password)
- //{
- IsValid = true;
- }
- return IsValid;
- }
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- db.Dispose();
- }
- base.Dispose(disposing);
- }
- }
- }
Add Comment
Please, Sign In to add comment