简体   繁体   中英

Block user from opening a form if other instance has this user

My WinForm Application must open specific form according to logged users. At the Login Form, the user authentication is completed, where user gives username-password. The valid credentials are inside a .csv file. If the user is valid, Login Form is hidden and Form2 appears.

What I want to do is to block a user who opens the application (Login Form) and gives the same username-password with someone who already runs this app. My current solution is to add to each process (instance) an environment variable with the login's username like this:

Process proc = System.Diagnostics.Process.GetCurrentProcess();
proc.StartInfo.EnvironmentVariables.Add("G2GUser",username);

And when another instance opens, checkes if the user who tries to log in is already uses the application. So, this variable is being checked:

List<Process> processes =     System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).ToList();
foreach (Process proc in processes)
{
   StringDictionary sd = proc.StartInfo.EnvironmentVariables;
   if (!String.IsNullOrEmpty(sd["G2GUser"]))
      if (sd["G2GUser"] == username) {; //block this user}
}

The problem is that I can see the processes' list, but EnvironmentVariables are not stored. Can you propose another way for this approach?

I wouldn't recommend using the environment variables.

Given you could have any arbitrary number of instances of the application you'll need to maintain some form of tracking these users.

You could communicate between the applications by making use of inter-process communication such as Named pipes see: Example named pipes IPC with read/write timeout

Then by communicating back and forth between the instances you could make decisions between them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM