禁用多个cookie值的覆盖

时间:2012-11-08 22:27:06

标签: c# asp.net vb.net

我注意到asp.net中有多个cookie的问题   当您在不同的回发中添加新值时,将删除较早的值,并且仅保留最后的值

- 例如(VB.net)   在Default1.aspx.vb

Response.Cookies("mycookie")("abc") = 123

在Default2.aspx.vb

Response.Cookies("mycookie")("def") = 456

现在如果执行default1,“abc”值将存储在“mycookie”中,但如果执行defualt2,则“abc”值将替换为新的“def”值!。

我尝试了以下解决方案,但我对此并不满意,因为我认为它需要大量资源而没有意义:   在Default2.aspx.vb

Dim OldValues As NameValueCollection = Request.Cookies.Get("mycookie").Values
Response.Cookies.Get("mycookie").Values.Clear()
Response.Cookies.Get("mycookie").Values.Add(OldValues)
Response.Cookies.Get("mycookie").Values.Add("def", 456)

现在,是否还有其他选项可以执行此操作,例如在设置中禁用某些内容或任何可防止覆盖之前值的内容?

谢谢:)

1 个答案:

答案 0 :(得分:0)

嗯,没有想法?!

无论如何,由于没有其他选择,我决定使用我的代码并做一些封装,对于公共benfit,我会在这里发布我的代码可能会让一些有兴趣的兄弟受益。

这里的主要思想是我开发了一个大型Web应用程序,我需要在多个级别范围内存储用户选择,例如按首选项保存订单,每页的项目数等等。以及在多级别范围内,例如保存长时间(在cookie中)或在短时间内(在会话中)的偏好。

由于这些选择太多,我被迫使用多个cookie,因为在某些浏览器中允许使用少量的Cookie。

这里是封装函数来做这个技巧:

Public Enum StoredPropertyScope As SByte
    SessionAndCookies = 0
    SessionOnly = 1
    CookiesOnly = 2
End Enum
Public Shared Function GetStoredProperty(ByVal group As String, ByVal Key As String, Optional scope As StoredPropertyScope = StoredPropertyScope.SessionAndCookies) As Object
    If (scope = StoredPropertyScope.SessionAndCookies Or scope = StoredPropertyScope.SessionOnly) And Not (HttpContext.Current.Session(group & "-" & Key) Is Nothing) Then
        Return HttpContext.Current.Session(group & "-" & Key)
    Else
        If (scope = StoredPropertyScope.SessionAndCookies Or scope = StoredPropertyScope.CookiesOnly) And Not (Request.Cookies(group) Is Nothing) Then
            Return Request.Cookies(group)(Key)
        Else
            Return Nothing
        End If
    End If
End Function
Public Shared Sub SetStoredProperty(ByVal group As String, ByVal key As String, value As String, Optional scope As StoredPropertyScope = StoredPropertyScope.SessionAndCookies)
    If scope = StoredPropertyScope.SessionAndCookies Or scope = StoredPropertyScope.CookiesOnly Then
        If Request.Cookies(group) Is Nothing Then
            Response.Cookies(group)(key) = value
            Response.Cookies(group).Expires = Now.AddYears(1)
        Else
            Dim OldValues As NameValueCollection = Request.Cookies.Get(group).Values
            OldValues.Item(key) = value
            Response.Cookies.Get(group).Values.Clear()
            Response.Cookies.Get(group).Values.Add(OldValues)
            Response.Cookies(group).Expires = Now.AddYears(1)
        End If
    End If
    If scope = StoredPropertyScope.SessionAndCookies Or scope = StoredPropertyScope.SessionOnly Then
        HttpContext.Current.Session(group & "-" & key) = value
    End If
End Sub
Public Overloads Shared Sub RemoveStoredProperty(ByVal group As String, ByVal key As String, Optional scope As StoredPropertyScope = StoredPropertyScope.SessionAndCookies)
    If scope = StoredPropertyScope.SessionAndCookies Or scope = StoredPropertyScope.CookiesOnly Then
        If Not (Request.Cookies(group) Is Nothing) AndAlso Request.Cookies(group).HasKeys Then
            Dim OldValues As NameValueCollection = Request.Cookies.Get(group).Values
            OldValues.Remove(key)
            Response.Cookies.Get(group).Values.Clear()
            Response.Cookies.Get(group).Values.Add(OldValues)
            Response.Cookies(group).Expires = Now.AddYears(1)
        End If
    End If
    If scope = StoredPropertyScope.SessionAndCookies Or scope = StoredPropertyScope.SessionOnly Then
        HttpContext.Current.Session.Remove(group & "-" & key)
    End If
End Sub
Public Overloads Shared Sub RemoveStoredProperty(ByVal group As String)
    'remove all from cookies only
    Response.Cookies(group).Expires = DateTime.Now.AddDays(-1)
End Sub
Public Shared Function CheckStoredProperty(ByVal group As String, ByVal key As String) As StoredPropertyScope
    'return where is property stored or -1 if not exists
    Dim result As StoredPropertyScope = -1
    If (Not Request.Cookies(group) Is Nothing) AndAlso (Not Request.Cookies(group)(key) Is Nothing) Then
        result = StoredPropertyScope.CookiesOnly
    End If
    If Not HttpContext.Current.Session(group & "-" & key) Is Nothing Then
        If result = -1 Then result = StoredPropertyScope.SessionOnly Else result = StoredPropertyScope.SessionAndCookies
    End If
    Return result
End Function

如何使用  1-首先你需要记录属性组和子键的所有名称,因为你将它用作字符串值,所以你有责任确保它正确。

2-存储值使用这样的代码:

SetStoredProperty("userSelections", "itemsOrderedBy", "Price", StoredPropertyScope.SessionAndCookies)
  • 第一个参数是组名,它将是cookie名称,第二个参数是proberty名称(cookie中的子值),第二个参数是problerty的值,第四个参数是可选的确定您要存储此值的位置(仅限会话或仅限cookie或两者)。

*注意(cookie过期日期为1年,您可以直接从代码中更改)

3-检索值:

GetStoredProperty("userSelections", "itemsOrderdBy",  StoredPropertyScope.CookiesOnly)
  • 第三个参数是可选的,如果你选择SessionAndCookies,返回值的优先级将是session

4-删除值:

RemoveStoredProperty("userSelections")  
RemoveStoredProperty("userSelections", "itemsOrderdBy", StoredPropertyScope.SessionAndCookies) 
  • 第一行将删除整个组(cookie),它仅用于cookie。第二行将从可选的第三个参数
  • 中确定的范围中删除指定的属性

5-存放财产的地方:

 CheckStoredProperty("userSelections", "itemsOrderdBy")
  • 此函数将搜索proberty,如果它存在将返回它的位置(StoredPropertyScope枚举),如果不存在则返回-1

我希望这会有用,如果有更好的解决方案,请告诉我。 感谢