asp.netsessionasp.net-membershipasp.net-profiles

Should I store user data in session or use a custom profile provider?


On my site when a user logs I create a session object with the following properties

DisplayName,
Email,
MemberId

Questions

  1. Would it make more sense to use a custom profile provider for holding user data?
  2. What are the pro's and con's of each approach (Session and custom profile provider)?
  3. Does it make sense to use a custom provider for read only data that can come from one or more tables?

Solution

  • My answer is not direct approach to your question. It is just an alternative approach.

    Instead of custom profile provider, I create custom Context to keep track of the current logged-in user's profile. Here is the sample code. You can store DisplayName, Email, MemberId stead of MyUser class.

    void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        if (HttpContext.Current.User != null && 
            HttpContext.Current.User.Identity.IsAuthenticated)
        {
            MyContext.Current.MyUser = YOURCODE.GetUserByUsername(HttpContext.Current.User.Identity.Name);
        }
    }
    
    public class MyContext
    {
        private MyUser _myUser;
    
        public static MyContext Current
        {
            get
            {
                if (HttpContext.Current.Items["MyContext"] == null)
                {
                    MyContext context = new MyContext();
                    HttpContext.Current.Items.Add("MyContext", context);
                    return context;
                }
                return (MyContext) HttpContext.Current.Items["MyContext"];
            }
            }
    
            public MyUser MyUser
            {
    
                get { return _myUser; }
                set { _myUser = value; }
            }
        }
    }