3

我有一个用于 Windows azure 身份验证的身份验证页面,其中身份验证在https://login.microsoftonline.com/login.srf?wa=wsignin1.0&wtrealm= ........ 成功身份验证后我们重定向到我们的网站。

我们需要删除在此 url 上生成的 cookie 以进行注销。任何机构都可以向我提供我们可以删除外部站点 cookie 的代码吗?

我们尝试使用以下代码进行删除,但未能成功。

public void deleteallcookies()
{
    int limit = Request.Cookies.Count; 
    HttpCookie aCookie;   //Instantiate a cookie placeholder
    string cookieName;   

    //Loop through the cookies
    for(int i = 0; i < limit; i++)
    {
        cookieName = Request.Cookies[i].Name;    //get the name of the current cookie
        aCookie = new HttpCookie(cookieName);    
         aCookie.Value = "";    //set a blank value to the cookie 
        aCookie.Expires = DateTime.Now.AddDays(-1);    

        Response.Cookies.Add(aCookie);    //Set the cookie
    }
}
4

1 回答 1

2

You want to get the cookie from the Response not the Request, i.e.

cookieName = Response.Cookies[i].Name; 

you can also enumerate slightly more elegantly as follows:

string[] myCookies = HttpContext.Current.Request.Cookies.AllKeys;
foreach (string cookieName in myCookies)
{
      var cookie = HttpContext.Current.Response.Cookies[cookieName];
      if (cookie != null)
      {
            cookie.Value = "";
            cookie.Expires = DateTime.Now.AddDays(-1);
      }
}
于 2013-09-16T10:27:41.320 回答