ASP.NET User.Identity.Name 替代方案?
我创建了自定义会员资格提供程序,对我来说,在 MembershipUser.ProviderUserKey
上操作比在 UserName 上操作更方便。因此,要检索 ProviderUserKey ,我执行这样的代码:
if (User.Identity.IsAuthenticated)
{
int UserID = (int)Membership.GetUser(User.Identity.Name).ProviderUserKey;
}
但是,当执行 GetUser() 方法时,必须从数据库检索用户数据,这让我很烦恼。这是不必要的服务器时间浪费,无论这个时间有多短,我都想避免它。
有没有其他方法可以更方便地获取 ProviderUserKey ,例如在 User.Identity.Name 情况下?
我想听听你的想法。您如何解决网页上的这个问题?
谢谢。
I've created custom membership provider and it is more convenient for me to operate on MembershipUser.ProviderUserKey
rather than UserName. So, to retrieve ProviderUserKey
I perform such code:
if (User.Identity.IsAuthenticated)
{
int UserID = (int)Membership.GetUser(User.Identity.Name).ProviderUserKey;
}
However, when GetUser()
method is executed, user data must be retrieved from database and this is bugging me. This is unnecessery waste of server time, no matter how short this time is, I would like to avoid it.
Is there any other way to get ProviderUserKey
in a more convenient way, like in User.Identity.Name
case?
I would like to hear your ideas. How do you solve this problem on your webpages?.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
会员 API 正在访问数据库,因为它是存储此信息的唯一位置。
User.Identity.Name
正在从每次请求时发送的 cookie 中获取记录的用户名。您可以实现自定义通用主体,并将必要的信息存储到在 Cookie 中加密的身份验证票证的userData
部分中。这是一篇涵盖此主题的文章 。The Membership API is hitting the database because it is the only place this information is stored.
User.Identity.Name
is fetching the logged username from a cookie which is sent at every request. You could implement a custom generic principal and store the necessary information into theuserData
part of the authentication ticket which is encrypted in the cookie. Here's an article which covers this topic.当他们第一次登录时从数据库读取
ProviderUserKey
并将其存储在用户的会话集合中?在后续请求中,您可以从会话中获取它,而无需访问数据库。Read
ProviderUserKey
from the database when they first log in and store it in the user's session collection? On subsequent requests you can just grab it from the session without going to the database.