首页 > 解决方案 > 在 MVC C# 中实现 PHP 会话

问题描述

目前,我正在使用 MVC C# 开发一个 Web 应用程序。我想问是否有办法在 MVC 中实现 PHP Session。我正在谈论的会话的使用就像,当你只写这样的链接或路径时,即使你没有使用帐户登录,你也不会被重定向到https://localhost:44360/Login/Login那个https://localhost:44360/Home/Home网页Home页。

登录帐户后,我被重定向到正确的主页,之后,我注销了帐户并尝试键入https://localhost:44360/Home/Home,但不幸的是它被重定向了,甚至单击了浏览器的后退按钮。

我目前正在处理下面的代码。

登录控制器.cs

 [HttpPost]
        public ActionResult Login(LoginModel userInfo, FormCollection collection, string returnUrl)
        {
            ILogicInterface<LogicalUserInput, LogicalSystemResult> iLogic = new UserLoginCheck();
            LogicalUserInput userInput = new LogicalUserInput();
            _ = new LogicalSystemResult();
            try
            {
                userInput[typeof(LoginModel).FullName] = userInfo;
                LogicalSystemResult systemResult = iLogic.DoProcess(userInput);
                bool userCheckExist = systemResult.ResultCode != LogicalSystemResult.RESULT_CODE_ERR_DATA_NOT_EXIST;

                if (userCheckExist)
                {
                    UserLoginModel loginModel = systemResult[typeof(UserLoginModel).FullName] as UserLoginModel;
                    Session["userInfo"] = loginModel;
                    FormsAuthentication.SetAuthCookie(loginModel.email, true);

                    if (!string.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        if (loginModel.AccountType == 0) {
                            return RedirectToAction("Home", "Home");
                        } else {
                            return RedirectToAction("Error", "Error");
                        }                   
                    }
                }
                else
                {
                    TempData.Clear();
                    TempData["Title"] = "Error!";
                    TempData["Message"] = " Invalid Username Or Password.";
                    return View();
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return RedirectToAction("Error", "Error");
            }
        }

HOME.cshtml 中的注销按钮

<a id="cmdLogOff">
   <i class="feather icon-log-out"></i>Logout
</a>

注销按钮的点击事件

<script>
    $("#cmdLogOff").on("click", function () {
        $("#HomeView").submit();
    });
</script>

@using (Ajax.BeginForm("LogOut",
         null,
         new AjaxOptions
         {
         },
    new { id = "HomeView" }))
{
   
}

HOMEController.cs

 [Authorize]
    public ActionResult Home()
    {
        if (Session["userInfo"] == null) {
            return RedirectToAction("Error", "Error");
        } else {
            UserLoginModel userLoginModel = Session["userInfo"] as UserLoginModel;
            TempData["user"] = userLoginModel.lastName + ", " + userLoginModel.firstName + " " + userLoginModel.middleName; 
            return View();
        }         
    }

    [Authorize]
    [HttpPost]
    public ActionResult LogOut() {
        try {
                Session.Abandon();
                FormsAuthentication.SignOut();
                Session["userInfo"] = null;
            return RedirectToAction("Login", "Login");
        } catch (Exception ex) {
            Console.WriteLine(ex.Message);
            return View();
        }
    }

这里的主要问题是,如果他/她只键入他/她想在没有帐户的情况下访问的链接,我怎么能不重定向用户。并且注销账户后,即使用户点击浏览器背面,主页也不会显示?

标签: c#asp.net-mvcauthenticationsession

解决方案


推荐阅读