我正在使用 aspx 和 c# 为登录设置身份验证 cookie。
FormsAuthentication.SetAuthCookie(UserName, True)
我想在同一个 cookie 中存储更多信息。我可以向此身份验证 cookie 添加值还是必须使用第二个 http cookie?
基本上我正在寻找存储用户的 Id
这样我就可以使用用户表行键访问数据库
谢谢, 伊甸园
最佳答案
您可以将用户数据添加到 FormsAuthenticationTicket,然后自己生成 cookie。
the MSDN documentation for FormsAuthenticationTicket 中有一个示例.
编辑
注意在创建ticket的时候需要设置超时时间,一般情况下你会希望和web.config中配置的值一样。遗憾的是,在 Framework 3.5 或更早版本中,FormsAuthentication
类并未公开此超时。对于解决方法,请使用对 this connect feedback item 的响应中描述的技术之一。 .
更新
That Connect feedback item is no longer there, sadly. Wish you had briefly described what the techniques were.
是的,很遗憾 Microsoft 丢弃了历史 Connect 项目。 IIRC,他们建议的两种技术是:
使用WebConfigurationManager读取相关配置部分并获取超时值。
使用
FormsAuthentication.GetAuthCookie
创建一个 cookie,使用FormsAuthentication.Decrypt
解密它并检查生成的FormsAuthenticationTicket
。
或者升级到具有 FormsAuthentication.Timeout
属性的 .NET 4.x。
参见 this question了解更多信息
关于c# - 使用 FormsAuthentication.SetAuthCookie 存储更多信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1149996/