关于如何在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