Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [HttpGet]
- public ActionResult LogIn()
- {
- return View();
- }
- [HttpPost]
- public ActionResult LogIn(Models.User user)
- {
- if (IsValid(user.Email, user.Password))
- {
- FormsAuthentication.SetAuthCookie(user.Email, false);
- return RedirectToAction("Index", "Home");
- }
- else
- {
- ModelState.AddModelError("", "Login details are wrong.");
- }
- return View(user);
- }
- [HttpGet]
- public ActionResult Register()
- {
- return View();
- }
- [HttpPost]
- public ActionResult Register(Models.User user)
- {
- try
- {
- if (ModelState.IsValid)
- {
- using (AppContext db = new AppContext())
- {
- var crypto = new SimpleCrypto.PBKDF2();
- var encrypPass = crypto.Compute(user.Password);
- var newUser = db.Users.Create();
- newUser.FirstName = user.FirstName;
- newUser.LastName = user.LastName;
- newUser.Email = user.Email;
- newUser.CompanyName = user.CompanyName;
- newUser.Password = encrypPass;
- newUser.PasswordSalt = crypto.Salt;
- newUser.AdminCode = 0;
- user.Password = encrypPass;
- user.PasswordSalt = crypto.Salt;
- db.Users.Add(newUser);
- db.SaveChanges();
- return RedirectToAction("Index", "Home");
- }
- }
- else
- {
- ModelState.AddModelError("", "Data is not correct");
- }
- }
- catch (DbEntityValidationException e)
- {
- foreach (var validationErrors in e.EntityValidationErrors)
- {
- foreach (var validationError in validationErrors.ValidationErrors)
- {
- Trace.TraceInformation(
- "Class: {0}, Property: {1}, Error: {2}",
- validationErrors.Entry.Entity.GetType().FullName,
- validationError.PropertyName,
- validationError.ErrorMessage);
- }
- }
- }
- return View();
- }
- private bool IsValid(string email, string password)
- {
- var crypto = new SimpleCrypto.PBKDF2();
- bool IsValid = false;
- using (AppContext db = new AppContext())
- {
- var user = db.Users.FirstOrDefault(u => u.Email == email);
- if (user != null)
- {
- if (user.Password == crypto.Compute(user.PasswordSalt, password))
- {
- IsValid = true;
- }
- }
- }
- return IsValid;
- }
- public ActionResult LogOut()
- {
- FormsAuthentication.SignOut();
- return RedirectToAction("Index", "Home");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement