我应该存储用户数据会话或使用自定义配置文件提供程序? [英] Should I store user data in session or use a custom profile provider?

查看:150
本文介绍了我应该存储用户数据会话或使用自定义配置文件提供程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 显示名称,
电子邮件,
会员ID

问题


  1. 难道让使用自定义配置文件提供用于保存用户更有意义
    数据?

  2. 什么是亲的和反对的每一种方法的(会话和自定义配置文件提供程序)?

  3. 是否有意义使用自定义提供了只读数据
    可以来自一个或多个表?


解决方案

我的答案是没有直接的方法来你的问题。它仅仅是一种替代方法。

相反,自定义配置文件提供的,我创建自定义的背景下,以跟踪当前登录用户的配置文件。下面是示例code。您可以存储显示名称,电子邮件,MYUSER类MEMBERID代替。

 无效Application_AuthenticateRequest(对象发件人,EventArgs的发送)
{
    如果(HttpContext.Current.User = NULL&放大器;!&安培;
        HttpContext.Current.User.Identity.IsAuthenticated)
    {
        MyContext.Current.MyUser =您的code.GetUserByUsername(HttpContext.Current.User.Identity.Name);
    }
}公共类MyContext
{
    私人MYUSER _myUser;    公共静态电流MyContext
    {
        得到
        {
            如果(HttpContext.Current.Items [MyContext] == NULL)
            {
                MyContext语境=新MyContext();
                HttpContext.Current.Items.Add(MyContext,背景);
                返回语境;
            }
            回报(MyContext)HttpContext.Current.Items [MyContext];
        }
        }        公共MYUSER MYUSER
        {            {返回_myUser; }
            集合{_myUser =价值; }
        }
    }
}

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?

解决方案

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

这篇关于我应该存储用户数据会话或使用自定义配置文件提供程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆