Reputation: 21
Have a Blazor Server web app and created a global class (using public static string). The string gets populated with the active directory username on initialization via the Index.razor page. However, all users who go to the website -- AFTER the first user has signed in -- see the FIRST users name instead of their own. Shouldn't this global class data be session specific? Below is the class (kept in the Data folder):
namespace HRJobApp.Data
{
public class UserAccess
{
public static string winGroup { get; set; } = "";
public static string winUsername { get; set; } = "";
}
}
Upvotes: 1
Views: 213
Reputation: 1802
There is a lot to unpack here, but as Henk pointed out, using static here will use the same value for the entire process. In a simple scenario, this isn't the functionality that you want. In a worse scenario, you open yourself up to very unpredictable and hard to resolve code when it acts right in some instances and doesn't in others.
This isn't really a bug, it's more of a design flaw. Without knowing your application or use-case... I would simply suggest removing the static keyword and add an instance of your class to the DI container with a scoped lifetime. See these Microsoft Docs for more information and how to inject it into your application. Depending on your level of experience, an alternative could be to create a wrapper class for your AD query and store a cached value there. You would register that as scoped as well. This approach would store the value closer to the source and make it more obvious how and when it gets set.
Upvotes: 0