Reputation: 2305
I'm trying to figure out which part of my program is causing this error.
I have multiple pages that all inherit from PageBase
. They get their user profile from PageBase
. This is the function that gets their user name from PageBase
:
uiProfile = ProfileManager.FindProfilesByUserName(CompanyHttpApplication.Current.Profile.UserName)
In the CompanyHttpApplication
I have
public static CompanyHttpApplication Current
{
get { return (CompanyHttpApplication)HttpContext.Current.ApplicationInstance; }
}
and
public CompanyProfileInfo Profile
{
get
{
return profile ??
(profile =
ProfileManager.FindProfilesByUserName(ProfileAuthenticationOption.Authenticated,
User.Identity.Name).Cast
<CompanyProfileInfo>().ToList().First());
}
private set { profile = value; }
}
Unfortunately I did not write this section of the code and the programmer who did it is no longer on the project. Is there any one that can explain to me why, when another user logs in (while I am using the application), I become that user?
Upvotes: 0
Views: 130
Reputation: 25137
The Application instance is shared across every request — the application level.
You want the Session level — each user gets their own instance.
Use HttpContext.Current.Session
instead of ApplicationInstance.
(Code below renames original, and adds a property, to be more clear. Feel free to adjust as necessary.)
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; }
}
Upvotes: 4
Reputation: 44642
HttpContext.Current.ApplicationInstance is globally shared. It is not per user. Thus, you have a shared profile that is immediately overwriting whatever you originally set when your new user logs in.
Upvotes: 5