public int Register(string email, string password, string foreignLanguage) { int foreignLanguageId = GetForeignLanguageByName(foreignLanguage); password = PasswordHashing.Hash(password); return(_accountRepository.Register(email, password, foreignLanguageId)); }
public ActionResult Login(UserCredentials user) { UserLogin login = user.UserLogin; ViewBag.Page = "Login"; string message = ""; using (DrinkDBEntities dc = new DrinkDBEntities()) { var queryResult = dc.Users.Where(u => u.Username == login.Username).FirstOrDefault(); if (queryResult != null) { if (string.Compare(PasswordHashing.Hash(login.Password), queryResult.Password) == 0) { #region Cookie to stay logged in int timeout = login.RememberMe ? 10000 : 60; var ticket = new FormsAuthenticationTicket(login.Username, login.RememberMe, timeout); string encrypted = FormsAuthentication.Encrypt(ticket); var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted); cookie.Expires = DateTime.Now.AddMinutes(timeout); cookie.HttpOnly = true; Response.Cookies.Add(cookie); #endregion return(RedirectToAction("Home", "Home")); } else { message = "Password is incorrect!"; } } else { message = "Invalid credentials"; } } ViewBag.Message = message; return(View("Account")); }
public bool AuthenticateUser(string username, string password, UserRoles role) { var user = _context.User.Include(u => u.Person).FirstOrDefault(u => u.Username == username); if (user == null) { return(false); } var accurateRole = role switch { UserRoles.Customer => _context.Customer.Any(customer => customer.PersonId == user.PersonId), UserRoles.Employee => _context.Employee.Any(employee => employee.PersonId == user.PersonId), _ => false }; var inputHash = PasswordHashing.Hash(password, user.Salt); var sameHash = inputHash.SequenceEqual(user.PasswordHash); return(accurateRole && sameHash); } }
public ActionResult Registration([Bind(Exclude = "IsVerified, ActivationCode, Role")] UserCredentials userCred) { ViewBag.Page = "Registration"; User user = new User(); user = userCred.UserRegistration; bool isSuccessful = false; string message = ""; if (ModelState.IsValid) { #region Check if email and username are taken var isExists = IsEmailExist(user.Email, user.Username); if (isExists[0]) { ModelState.AddModelError("EmailExists", "Email already Exists"); return(View("Account")); } if (isExists[1]) { ModelState.AddModelError("UsernameExists", "Username is taken!"); return(View("Account")); } #endregion #region Generate Activation Code user.ActivationCode = Guid.NewGuid(); #endregion #region Pw hashing user.Password = PasswordHashing.Hash(user.Password); user.ConfirmPassword = PasswordHashing.Hash(user.ConfirmPassword); #endregion user.isVerified = false; user.Role = "user"; //cHxY2:)CoCkTaIlFiNdeR #region Save to DB using (DrinkDBEntities dc = new DrinkDBEntities()) { dc.Users.Add(user); dc.SaveChanges(); SendEmailVerification(user.Username, user.Email, user.ActivationCode.ToString()); message = "Successful Registration! Check your email and verify your account!"; isSuccessful = true; } #endregion } else { message = "Invalid request!"; } ViewBag.Status = isSuccessful; ViewBag.Message = message; return(View("Account")); }
public int LogIn(string email, string password) { password = PasswordHashing.Hash(password); return(_accountRepository.LogIn(email, password)); }