开发者

Clearing multiple cookies

The code below tries to clear the cookies for all domains once a user logs out of the system. For some reason, only the last domain in the array is cleared. Why does this happen? Am I doing something wrong?

For example, if I change the size of the array to 4 and then change the for loop to only go to 3, then it only logs me out of y.xcv.com.

As a sidenote, I have this loop working on a different server that uses a slightly different function to clear the cookies.

Edit: Code updated per suggestions below. Now it fails on the "as HttpCookie" line. Do I need to include some library?

Dim aDomain(12)
Dim ESidCookie, WIdCookie, EBidCookie, TSidAccessCookie, PSidAccessCookie, SSidCookie As HttpCookie

aDomain(0) = ".x.com"
aDomain(1) = "y.x.com"
aDomain(2) = "y.x.com"
aDomain(3) = "y.xcv.com"
aDomain(4) = "x.com"
aDomain(5) = "y.z.a.com"
aDomain(6) = "y.z.a.com"
aDomain(7) = "z.a.com"
aDomain(8) = ""
aDomain(9) = "y.x.com"
aDomain(10) = "y.x.com" 
aDomain(11) = "y.x.com"

for count = 0 to 11
    strDomain = aDomain(count)
    response.Write count & "/" & strDomain

    ESidCookie = New HttpCookie("oneCookie")
    ESidCookie.Domain = strDomain
    ESidCookie.Path = "/"
    ESidCookie = ""
    ESidCookie.Expires = now() - 100
    Response.Cookies.Add(ESidCookie)

    WIdCookie = New HttpCookie("twoCookie")
    WIdCookie.Domain = strDomain
    WIdCookie.Path = "/"
    WIdCookie = ""
    WIdCookie.Expires = now() - 100开发者_JS百科
    Response.Cookies.Add(WIdCookie)

    EBidCookie = New HttpCookie("threeCookie")
    EBidCookie.Domain = strDomain
    EBidCookie.Path = "/"
    EBidCookie = ""
    EBidCookie.Expires = now() - 100
    Response.Cookies.Add(EBidCookie)

    TSidAccessCookie = New HttpCookie("fourCookie")
    TSidAccessCookie.Path = "/"
    TSidAccessCookie = "LoggedOut"
    Response.Cookies.Add(TSidAccessCookie)

    PSidAccessCookie = New HttpCookie("fiveCookie")
    PSidAccessCookie.Domain = strDomain
    PSidAccessCookie.Path = "/"
    PSidAccessCookie = ""
    PSidAccessCookie.Expires = now() - 100
    Response.Cookies.Add(PSidAccessCookie)

    SSidCookie = New HttpCookie("sixCookie")
    SSidCookie.Domain = strDomain
    SSidCookie.Path = "/"
    SSidCookie = ""
    SSidCookie.Expires = now() - 100
    Response.Cookies.Add(SSidCookie)
next

Any help is appreciated. Thanks!


The Response.Cookies collection is keyed off of the cookie name so you are just changing the domain of the same cookie each time you go through your loop. That's why the last one wins.

You could try creating a new cookie object and adding that to the Response.Cookies collection in your loop instead.


If you want to clear all cookies you will should create all new ones with the same name. Here is a basic example that should get you going:

Dim newCookie As HttpCookie
For i As Integer = 0 To 10
    ' creating a new cookie each time
    newCookie = New HttpCookie(cookieNames(i))
    ' expire the cookie
    newCookie.Expires = DateTime.Now.AddDays(-1)
    ' storing the new cookie each time
    Response.Cookies.Add(newCookie)
Next

It doesn't look like your creating all new cookies and adding them to the response properly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜