Wodzu
Wodzu

Reputation: 6979

ASP.NET User.Identity.Name alternative?

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.

Upvotes: 1

Views: 2720

Answers (2)

batwad
batwad

Reputation: 3655

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.

Upvotes: 3

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

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 the userData part of the authentication ticket which is encrypted in the cookie. Here's an article which covers this topic.

Upvotes: 5

Related Questions