Session or Query
Should I store 开发者_运维技巧the user's ID, name & email address in a session variable or should I query for the user's user's ID, name & email address everytime i need to use it?
public class UserInfo
{
public int UserID {get;set;}
public string Email {get;set;}
...
}
When a user logs on, create an instance of UserInfo and store it in the session.
Keep it in a session variable. Its better to cache your frequently used data in the application instead of having a bunch of round-trips to the server.
If you are using ASP.NET security, i.e. membership providers and role providers etc, then you don't have to store it. It is provided by the membership provider:
MembershipUser myObject = Membership.GetUser();
string UserID = myObject.ProviderUserKey.ToString();
Furthermore, if you are authenticating the user on each page, which happens automatically on a secure website, then the Membership provider doesn't have to go back to the database to get the data because it is already in scope for the current request.
If you are using the out-of-the-box profile provider, I would encourage you to use and download the table based provider mentioned in Scott Gu's blog: http://weblogs.asp.net/scottgu/archive/2006/01/10/435038.aspx It is a lot more friendly and performs a lot better than the default provider.
精彩评论