0

我试图找出我的程序的哪一部分导致了这个错误。

我有多个页面都继承自PageBase. 他们从PageBase. 这是从以下位置获取用户名的函数PageBase

uiProfile = ProfileManager.FindProfilesByUserName(CompanyHttpApplication.Current.Profile.UserName)

CompanyHttpApplication我有

    public static CompanyHttpApplication Current
    {
        get { return (CompanyHttpApplication)HttpContext.Current.ApplicationInstance; }
    }

    public CompanyProfileInfo Profile
    {
        get
        {
            return profile ??
                   (profile =
                    ProfileManager.FindProfilesByUserName(ProfileAuthenticationOption.Authenticated,
                                                          User.Identity.Name).Cast
                        <CompanyProfileInfo>().ToList().First());
        }
        private set { profile = value; }
    }

不幸的是,我没有编写这部分代码,并且编写它的程序员不再参与该项目。有没有人可以向我解释为什么当另一个用户登录时(当我使用应用程序时),我成为那个用户?

4

2 回答 2

5

HttpContext.Current.ApplicationInstance 是全局共享的。它不是每个用户。因此,您拥有一个共享配置文件,该配置文件会在新用户登录时立即覆盖您最初设置的任何内容。

于 2012-09-28T20:03:03.337 回答
4

Application 实例在每个请求(应用程序级别)之间共享。

您需要 Session 级别——每个用户都有自己的实例。

使用HttpContext.Current.Session而不是ApplicationInstance

(下面的代码重命名了原始代码,并添加了一个属性,以便更清楚。根据需要随意调整。)

public static CompanyHttpApplication CurrentApplication
{
    // store application constants, active user counts, message of the day, and other things all users can see
    get { return (CompanyHttpApplication)HttpContext.Current.ApplicationInstance; }
}

public static Session CurrentSession
{
    // store information for a single user — each user gets their own instance and can *not* see other users' sessions
    get { return HttpContext.Current.Session; }
}
于 2012-09-28T20:05:50.977 回答