【问题标题】:How to clear all the session variables when i am logging out in mvc4?当我在 mvc4 中注销时如何清除所有会话变量?
【发布时间】:2014-03-13 10:34:25
【问题描述】:

我是 Asp.Net Mvc4 的新手。我想在注销时清除所有会话变量。我正在使用此代码。但它不起作用。请帮我搞定它。提前致谢。

我的控制器代码:

public ActionResult Logout()
        {
            System.Web.Security.FormsAuthentication.SignOut();
            Session.Clear();
            Session.RemoveAll();
            return RedirectToAction("Index", "Login");
        }

【问题讨论】:

  • 可能会话在您退出后就消失了。在调用 SignOut 之前尝试清理会话?

标签: asp.net-mvc-4 session asp.net-mvc-routing


【解决方案1】:

也尝试清除 cookie:

// clear authentication cookie
HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
cookie1.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(cookie1);

// clear session cookie
HttpCookie cookie2 = new HttpCookie("ASP.NET_SessionId", "");
cookie2.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(cookie2);

【讨论】:

    猜你喜欢
    • 2013-04-08
    • 2023-03-14
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 2012-06-04
    • 2017-03-01
    • 2017-05-27
    相关资源
    最近更新 更多