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

回眸只為那壹抹淺笑 提交于 2019-12-08 09:58:44

问题


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?

回答1:


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; }
        }
    }
}


来源:https://stackoverflow.com/questions/9832141/should-i-store-user-data-in-session-or-use-a-custom-profile-provider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!