如何在ASP.NET Membership Cookie中存储自定义数据

前端之家收集整理的这篇文章主要介绍了如何在ASP.NET Membership Cookie中存储自定义数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
关于如何在ASP.NET会员cookie中存储自定义数据,任何人都可以给我一个例子(或指出我正确的方向)吗?

我需要向cookie添加一些自定义属性,如UserID和URLSlug,并能够以检索用户名的相同方式检索信息.

编辑:

我使用了Code Poet的例子并想出了以下内容.

当我在Dim SerializedUser As String = SerializeUser(userData)设置断点时,userData的值是正确的.它具有我期望它拥有的所有属性.

我现在遇到的问题是,当我到达Dim userdata As String = authTicket.UserData(断点)时,值为“”.我想知道我做错了什么.

这是代码.

Imports System
Imports System.Web
Imports System.Web.Security

Namespace Utilities.Authentication
    Public NotInheritable Class CustomAuthentication
        Private Sub New()
        End Sub

        Public Shared Function CreateAuthCookie(ByVal userName As String,ByVal userData As Domain.Models.UserSessionModel,ByVal persistent As Boolean) As HttpCookie

            Dim issued As DateTime = DateTime.Now
            ''# formsAuth does not expose timeout!? have to hack around the
            ''# spoiled parts and keep moving..
            Dim fooCookie As HttpCookie = FormsAuthentication.GetAuthCookie("foo",True)
            Dim formsTimeout As Integer = Convert.ToInt32((fooCookie.Expires - DateTime.Now).TotalMinutes)

            Dim expiration As DateTime = DateTime.Now.AddMinutes(formsTimeout)
            Dim cookiePath As String = FormsAuthentication.FormsCookiePath

            Dim SerializedUser As String = SerializeUser(userData)

            Dim ticket = New FormsAuthenticationTicket(0,userName,issued,expiration,True,SerializedUser,cookiePath)
            Return CreateAuthCookie(ticket,persistent)
        End Function

        Public Shared Function CreateAuthCookie(ByVal ticket As FormsAuthenticationTicket,ByVal expiration As DateTime,ByVal persistent As Boolean) As HttpCookie
            Dim creamyFilling As String = FormsAuthentication.Encrypt(ticket)
            Dim cookie = New HttpCookie(FormsAuthentication.FormsCookieName,creamyFilling) With { _
             .Domain = FormsAuthentication.CookieDomain,_
             .Path = FormsAuthentication.FormsCookiePath _
            }
            If persistent Then
                cookie.Expires = expiration
            End If

            Return cookie
        End Function


        Public Shared Function RetrieveAuthUser() As Domain.Models.UserSessionModel
            Dim cookieName As String = FormsAuthentication.FormsCookieName
            Dim authCookie As HttpCookie = HttpContext.Current.Request.Cookies(cookieName)
            Dim authTicket As FormsAuthenticationTicket = FormsAuthentication.Decrypt(authCookie.Value)
            Dim userdata As String = authTicket.UserData

            Dim usersessionmodel As New Domain.Models.UserSessionModel
            usersessionmodel = DeserializeUser(userdata)
            Return usersessionmodel
        End Function


        Private Shared Function SerializeUser(ByVal usersessionmodel As Domain.Models.UserSessionModel) As String
            Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
            Dim mem As New IO.MemoryStream
            bf.Serialize(mem,usersessionmodel)
            Return Convert.ToBase64String(mem.ToArray())
        End Function

        Private Shared Function DeserializeUser(ByVal serializedusersessionmodel As String) As Domain.Models.UserSessionModel
            Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
            Dim mem As New IO.MemoryStream(Convert.FromBase64String(serializedusersessionmodel))
            Return DirectCast(bf.Deserialize(mem),Domain.Models.UserSessionModel)
        End Function
    End Class
End Namespace

这就是我创造所有魔力的地方.此方法位于继承System.Web.Mvc.Controller的“BaseController”类中

Protected Overrides Function CreateActionInvoker() As System.Web.Mvc.IActionInvoker

            If User.Identity.IsAuthenticated Then ''# this if statement will eventually also check to make sure that the cookie actually exists.

                Dim sessionuser As Domain.Models.UserSessionModel = New Domain.Models.UserSessionModel(OpenIdService.GetOpenId(HttpContext.User.Identity.Name).User)
                HttpContext.Response.Cookies.Add(UrbanNow.Core.Utilities.Authentication.CustomAuthentication.CreateAuthCookie(HttpContext.User.Identity.Name,sessionuser,True))
            End If
End Function

这是我尝试检索信息的方式.

Dim user As Domain.Models.UserSessionModel = CustomAuthentication.RetrieveAuthUser

解决方法

根据具体情况,使用单独的cookie可能是一个可行的选择,但在我看来,由于几个原因,包括简单的事实,您必须管理多个cookie以及管理cookie的生命周期,这是个最佳选择.

自定义信息合并到表单票证的最可靠策略是利用票证的userData字段.这正是它的用途.

您可以轻松地将自定义数据存储在故障单的userData字段中.

关于要在here中解释的票证中存储的数据的大小,需要注意一些问题

here是一个小类,可以帮助您在表单票证中存储自定义数据.

原文链接:https://www.f2er.com/aspnet/251776.html

猜你在找的asp.Net相关文章