将自定义用户信息存储在身份验证票证中而不是会员资格提供程序中
我一直在考虑实现一个基于 SQL Server 的自定义成员资格提供程序,我的问题之一是membershipUserObject 以 GUID 为键。 由于我使用现有 ID 作为角色和用户数据的密钥,因此这提出了一个有趣的问题。
如果我想在 Web 会话中保留自己的 ID,而不需要不断地往返于数据库,我想听听您对使用哪个选项(或者是否还有其他我没有考虑过的选项)的意见。 我知道,默认情况下,登录控件会使用成员资格对象的用户名创建一个表单身份验证 cookie。 所以 - 我可以:
- 实现登录控件的 Logging_In 方法以手动将我的字段添加到身份验证 cookie
if (Membership.ValidateUser(Login1.用户名, Login1.密码)) { FormsAuthenticationTicket 票证 = 新 表单身份验证票( 1、 登录1.用户名, 日期时间.现在, 日期时间.Now.AddMinutes(30), Login1.RememberMeSet,
“一些自定义数据想要存储在票证中......”,//用户数据, 在这种情况下的角色 FormsAuthentication.FormsCookiePath);
字符串哈希 = FormsAuthentication.Encrypt(ticket); HttpCookie cookie = 新的 HttpCookie( FormsAuthentication.FormsCookieName, 哈希);
if (ticket.IsPersistent) cookie.Expires = Ticket.Expiration;
Response.Cookies.Add(cookie);
创建一个继承自 MembershipUser 的自定义 MembershipUser 对象并提供我的额外属性。 我仍然必须以某种方式保留它(在会话中?呃..)
创建一个包含额外字段的自定义配置文件提供程序并在会话中缓存该字段。 不过,对于我实际缓存的几个字段来说,这似乎有点矫枉过正。
这里的最佳实践是什么? 我读过无数文章,表单中的额外数据似乎是迄今为止最好的。
I've been looking at implementing a custom SQL Server-based membership provider and one of my problems is that the membershipUserObject is keyed on a GUID. Since I'm using an existing ID as a key to roles and user data, this presents an interesting problem.
I'd like your opinion on which option -- or whether there's another option I haven't considered -- to use if I want to persist my own ID in a web session without round-tripping to the database constantly. I know that, by default, the login control creates a Forms Authentication cookie with the username of the membership object. So -- I could:
- Implement the Logging_In method of the login control to add my field to the authentication cookie manually
if (Membership.ValidateUser(Login1.UserName, Login1.Password))
{
FormsAuthenticationTicket ticket = new
FormsAuthenticationTicket(
1,
Login1.UserName,
DateTime.Now,
DateTime.Now.AddMinutes(30),
Login1.RememberMeSet,"some custom data want to store in ticket....", // User-data,
in this case the roles
FormsAuthentication.FormsCookiePath);string hash = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(
FormsAuthentication.FormsCookieName,
hash);if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;
Response.Cookies.Add(cookie);
Create a custom MembershipUser Object that inherits from MembershipUser and provides my extra property. I'll still have to persist it somehow (in Session? eww..)
Create a custom Profile Provider that contains the extra field and cache that in session. This seems a bit like overkill, though, for the few fields I would actually cache.
What's the best practice here? I've read countless articles and the extra data in the forms ticket seems like the best so far.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MembershipUser.ProviderUserKey 是对象类型,使用默认提供程序,我们将其转换为 Guid 以便使用它。
由于您使用的是自定义会员资格提供程序,因此当您从数据库检索用户时,您不能用 int (或您的 id 的任何类型)填充 MembershipUser.ProviderUserKey 吗? 当然,每次您想使用 MembershipUser.ProviderUserKey 时,您都必须将其转换为 int 。
The MembershipUser.ProviderUserKey is of type object, using the default provider we cast this to a Guid in order to use it.
Since you are using a custom membership provider, when you retrieve a user from the database couldn't you populate MembershipUser.ProviderUserKey with an int (or whatever type your id is)? Of course, you would have to cast MembershipUser.ProviderUserKey to an int every time you wanted to use it.