| 对HttpCookie)
Public Shared Sub SetCookie(ByVal cookie As HttpCookie)
HttpContext.Current.Response.Cookies.Set(cookie)
End Sub
''''获取COOKIE *****************************************************
Public Shared Function GetTripleDESEncryptedCookieValue(ByVal key As String) _
As String
''''只对密钥加密
key = CryptoUtil.EncryptTripleDES(key)
''''获取Cookie值
Dim value As String
value = GetCookieValue(key)
''''解密Cookie值
value = CryptoUtil.DecryptTripleDES(value)
Return value
End Function
Public Shared Function GetEncryptedCookieValue(ByVal key As String) As String
''''只对密钥加密
key = CryptoUtil.Encrypt(key)
''''获取Cookie值
Dim value As String
value = GetCookieValue(key)
''''解密Cookie值
value = CryptoUtil.Decrypt(value)
Return value
End Function
Public Shared Function GetCookie(ByVal key As String) As HttpCookie
''''编码密钥
key = HttpContext.Current.Server.UrlEncode(key)
Return HttpContext.Current.Request.Cookies.Get(key)
End Function
Public Shared Function GetCookieValue(ByVal key As String) As String
Try
''''编码在GetCookie里完成
''''获取Cookie值
Dim value As String
value = GetCookie(key).Value
''''解码所存储的值
value = HttpContext.Current.Server.UrlDecode(value)
Return value
Catch
End Try
End Function
End Class
上面的设置功能中,有些功能附加提供了Cookie有效期这个参数。不设置该参数,Cookie将只为浏览器会话才保存在内存中。为了设置永久的Cookie,就需要设置有效期参数。
上面我们对密钥和Cookies值进行了编码与解码,其原因是Cookies与URLs有同样的限制,字符“=”和“;”是保留的,不能使用。这在保存加密后的数据时尤其重要,因为加密算法将添加“=”,按所分配块的大小来填满该数据块。
好了,你会保护Cookies数据了吧?
上一页 [1] [2] |