I have a Multi User application I am converting to C#. Once the user is logged in He may execute different menu options in the application. For each form the user logs into, the first thing I need to do is validate his ability to use that function and at what rights level,
I have a User Object which can be called by the the UserID (integer), but I ma not sure if that can/should be done, Or if there is another way to accomplish that goal.
In other words I need to know what the user ID is, with him logging in only once at the start of the application.
Any help would be greatly appreciated.
Ed

Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 27, 2024, 12:34 AM EDT
In a multi-user application, it’s common to store the logged-in user’s information in a globally accessible location after they log in. This way, you can access this information from anywhere in your application.
In C#, one way to do this is to create a static class that holds the User object. Here’s an example:
public static class Global { public static User LoggedInUser { get; set; } }In your login method, after the user has been authenticated, you can set the
LoggedInUserproperty:public void Login(string username, string password) { // Authenticate user User user = Authenticate(username, password); if (user != null) { Global.LoggedInUser = user; } }Now, you can access the logged-in user’s information from anywhere in your application like this:
You can then use this
currentUserobject to check the user’s permissions when they try to access a particular function.Please note that this is a simple example and might not be suitable for all applications. Depending on your application’s architecture and requirements, you might need a more sophisticated approach to manage user sessions and permissions. For example, in a web application, you might want to store the user’s information in a session variable or a secure cookie instead of a static variable.
Thanks