ASP.Net 删除/过期会话 cookie

Gle*_*ong 2 c# asp.net cookies session

我们有许多公司内部的 ASP.Net 应用程序。全部使用表单身份验证,并且都是基于会话的...

我想要做的是当用户退出一个应用程序时,他/她会退出所有应用程序。

我有一些迭代 cookie 集合的逻辑。我可以看到所有其他 ASP.Net 应用程序,但我无法删除它们。

我目前使用以下逻辑:

// expire all asp.net app tickets
        string[] allDomainCookes = HttpContext.Current.Request.Cookies.AllKeys;

        foreach (string domainCookie in allDomainCookes)
        {
            if (domainCookie.Contains("ASPXAUTH"))
            {
                var expiredCookie = new HttpCookie(domainCookie) { Expires = DateTime.Now.AddDays(-1) };
                HttpContext.Current.Response.Cookies.Add(expiredCookie);
            }
        }
        HttpContext.Current.Request.Cookies.Clear();
Run Code Online (Sandbox Code Playgroud)

由于某种原因,它们没有被删除。我知道它们都在那里,因为我已经把它们写在了页面上。它们只是没有被删除......这是因为这些是会话 cookie 吗?

另外我应该补充一下,它们都是某个域的子域,所以所有权应该不是问题?

sye*_*ash 5

试试这个代码..对我有用

            FormsAuthentication.SignOut();
            HttpContext.Current.Session.Clear();
            HttpContext.Current.Session.Abandon();
            HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
            cookie1.Expires = DateTime.Now.AddYears(-1);
            HttpContext.Current.Response.Cookies.Add(cookie1);
            HttpCookie cookie2 = new HttpCookie("ASP.NET_SessionId", "");
            cookie2.Expires = DateTime.Now.AddYears(-1);
            HttpContext.Current.Response.Cookies.Add(cookie2);
Run Code Online (Sandbox Code Playgroud)