コード例 #1
0
        public ActionResult UserLogin(LogInVM entity)
        {
            string OldHASHValue = string.Empty;

            byte[] SALT = new byte[GenerateSaltKey._SaltLengthLimit];

            try
            {
                using (db = new CodeliteEntities1())
                {
                    // 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.user_Credential.Where(s => s.user_Name == entity.UserName.Trim()).FirstOrDefault();

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

                    bool isLogin = LogInVM.CompareHashValue(entity.Password, entity.UserName, OldHASHValue, SALT);

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

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

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