【发布时间】:2016-11-03 10:12:01
【问题描述】:
我在我的 ASP.NET MVC 应用程序中使用 ASP.NET 身份并使用 cookie 中间件进行身份验证和授权。但我意识到我的 cookie 存储在浏览器 cookie 存储中的时间很长。因此,我决定将其存储到 StateServer 中。为此,我在Startup 类中使用以下代码
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
SlidingExpiration = true,
SessionStore = new AspNetAuthSessionStore()
});
SessionStore 属性需要IAuthenticationSessionStore 类型,我发现了in this link
但我在存储 cookie 数据时遇到以下错误
无法序列化会话状态。在“状态服务器”和 'SQLServer' 模式,ASP.NET 将序列化会话状态对象, 结果是不可序列化的对象或 MarshalByRef 对象 不允许。如果类似的序列化,同样的限制适用 由自定义会话状态存储在“自定义”模式下完成。
我看到上述类 AspNetAuthSessionStore 中的 implementationIAuthenticationSessionStore.StoreAsync() 存储 cookie 存在一些问题
public Task<string> StoreAsync(AuthenticationTicket ticket)
{
string key = Guid.NewGuid().ToString();
HttpContext httpContext = HttpContext.Current;
CheckSessionAvailable(httpContext);
httpContext.Session[key + ".Ticket"] = ticket;
return Task.FromResult(key);
}
我认为在httpContext.Session[key + ".Ticket"] = ticket; 行中分配的对象应该是一个可序列化的对象,而AuthenticationTicket 类不是。
那么,我该如何解决这个问题。
【问题讨论】:
-
你控制
AuthenticationTicket。在其上添加Serializable属性。 -
@AmitKumarGhosh 我会这样做的。不幸的是它属于
Microsoft.Owin.Security图书馆。
标签: c# asp.net-mvc serialization asp.net-identity