如何在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是一个小类,可以帮助您在表单票证中存储自定义数据. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc-3 – 为什么Html.Label()不适用于句点?
- ASP.NET -- 获取浏览器信息
- 为什么32位应用程序池在IIS中更有效?
- asp.net – 使用Wopi和Office Online Server在Word中编辑
- asp.net-web-api – 如何在ASP.NET WebAPI RC中绑定一个简单
- asp.net-mvc – FileContentResult和国际字符
- asp.net-mvc-3 – 使用MVC3中的模型绑定ListBox
- asp.net-mvc – 如何与Entity Framework创建外键关系?
- asp.net – 如何追踪过期的WIF fedauth cookies?
- asp.net-mvc – Kendo:处理Ajax数据请求中的错误
- asp.net-mvc – ASP.NET MVC是否有分页解决方案,
- asp.net-mvc – ASP MVC 3支持多行编辑
- asp.net – 处理“潜在的危险Request.Form值…”
- asp.net-mvc – 刷新数据库更改页面
- asp.net – Dotnet核心2.0认证多个模式身份cooki
- Asp.net 实现Session分布式储存(Redis,Mongodb,M
- asp.net – CSS更改不反映在网站上
- asp.net-mvc – 需要经过身份验证的用户的Ajax调
- asp.net-mvc – ASP.NET MVC解决方案组织
- asp.net-mvc – 使用ui-router的.NET MVC和Angul