Example #1
0
        public ActionResult Login(LoginVM entity)
        {
            string OldHASHValue = string.Empty;

            byte[] SALT = new byte[saltLengthLimit];

            try
            {
                using (db = new SetlCityDBEntities())
                {
                    // Ensure we have a valid viewModel to work with
                    if (!ModelState.IsValid)
                    {
                        return(View(entity));
                    }

                    //Retrive Stored HASH Value From Database According To Username (one unique field)
                    var userInfo = db.Users.Where(s => s.Name == entity.Username.Trim()).FirstOrDefault();

                    //Assign HASH Value
                    if (userInfo != null)
                    {
                        OldHASHValue = userInfo.Hash;
                        SALT         = userInfo.Salt;
                    }

                    bool isLogin = Hashing.CompareHashValue(entity.Password, entity.Username, OldHASHValue, SALT);

                    if (isLogin)
                    {
                        //Login Success
                        //For Set Authentication in Cookie (Remeber ME Option)
                        SignInRemember(entity.Username, entity.isRemember);

                        //Set A Unique ID in session
                        Session["UserID"] = userInfo.Id;

                        // If we got this far, something failed, redisplay form
                        // return RedirectToAction("Index", "Dashboard");
                        return(RedirectToLocal(entity.ReturnURL));
                    }
                    else
                    {
                        //Login Fail
                        TempData["ErrorMSG"] = "Access Denied! Wrong Credential";
                        return(View(entity));
                    }
                }
            }
            catch
            {
                throw;
            }
        }