Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with static field in asp.net web-site

Tags:

c#

.net

asp.net

I'm currently working on an ASP.NET web-site. In one of my pages, I had a static field for the current logged in user's CompanyId.

private static Guid _CompanyId = Company.Get().CompanyId;

Company.Get() returns the information about the company of the currently logged in user, where the UserId is retrieved using:

System.Web.Security.Membership.GetUser();

But when logging in as another user, in antoher company, Company.Get().CompanyId would return the Guid from the first company.

Have I missed the point of using static fields, or does this have another cause? I fixed it, by replacing all the references to _CompanyId in my code-behind with the Company.Get().CompanyId for a quick fix, but this is not really a good solution.

like image 421
sshow Avatar asked Dec 22 '22 09:12

sshow


2 Answers

static variable value persist at the application level and hence across user wise. you should use session to store your information. static variable value is not change until you reassign the value, application restart, etc..

like image 52
Muhammad Akhtar Avatar answered Jan 05 '23 20:01

Muhammad Akhtar


You should use HttpContext.Current to store session level variables. static variables are visible to all sessions in your web application.

like image 34
Joe Caffeine Avatar answered Jan 05 '23 19:01

Joe Caffeine