예제 #1
0
        public ActionResult Login(LoginDetailsVM loginData)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("", "Invalid Username or Password.");
                return(View(loginData));
            }

            if (!dbContext.Logins.Any(m => m.Username == loginData.UserName))
            {
                ModelState.AddModelError("", "Username does not exist.");
                return(View(loginData));
            }
            else
            {
                try
                {
                    var userLoginDetails = dbContext.Logins.FirstOrDefault(m => m.Username == loginData.UserName);
                    if (userLoginDetails != null)
                    {
                        var    salt          = userLoginDetails.Salt;
                        string enterPassword = loginData.Password;
                        string savedPassword = userLoginDetails.Password;

                        if (PasswordSecurity.IsValid(enterPassword, salt, savedPassword))
                        {
                            if (Session.Count == 0)
                            {
                                Session["UserID"]   = userLoginDetails.UserID;
                                Session["Username"] = userLoginDetails.Username;
                                return(RedirectToAction("Index", "Resume"));
                            }
                            else
                            {
                                ModelState.AddModelError("", "Session already exists. Try Again.");
                                return(View(loginData));
                            }
                        }
                        else
                        {
                            throw new UnauthorizedAccessException();
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "User not found.");
                        return(View(loginData));
                    }
                }
                catch (UnauthorizedAccessException)
                {
                    ModelState.AddModelError("", "Wrong Password. Try Again.");
                    return(View(loginData));
                }
                catch (Exception)
                {
                    ModelState.AddModelError("", "Oops!!! Something went wrong. Try Again.");
                    return(View(loginData));
                }
            }
        }