【问题标题】:Should I store user data in session or use a custom profile provider?我应该在会话中存储用户数据还是使用自定义配置文件提供程序?
【发布时间】:2013-08-06 23:18:54
【问题描述】:

当用户登录时,我会在我的网站上创建一个具有以下属性的会话对象

DisplayName,
Email,
MemberId

问题

  1. 使用自定义配置文件提供程序来保留用户是否更有意义 数据?
  2. 每种方法(会话和自定义配置文件提供程序)的优缺点是什么?
  3. 为只读数据使用自定义提供程序是否有意义? 可以来自一张或多张桌子?

【问题讨论】:

  • 我不明白您的问题:会话因用户放弃或空闲 20 分钟而过期。如果您需要持久保存数据,您可以使用 .Net Profile 将这些信息存储在数据库中。
  • 我不知道你不明白什么

标签: asp.net session asp.net-membership asp.net-profiles


【解决方案1】:

我的回答不是直接回答您的问题。这只是一种替代方法。

我创建了自定义上下文来跟踪当前登录用户的配置文件,而不是自定义配置文件提供程序。这是示例代码。您可以存储 DisplayName、Email、MemberId 而不是 MyUser 类。

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

【讨论】:

    猜你喜欢
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 2012-01-15
    • 2010-09-21
    • 2011-09-05
    • 2019-11-11
    • 2016-07-01
    • 1970-01-01
    相关资源
    最近更新 更多