How to save user login data in a Winforms application?

Hi, I can be very new to windows. Here I want to save the state (for example, a session in web applications) as windows.

In fact, I want to save the user login information to the session. But I think that in winforms there is no concept of a session. So what is an alternative method of handling this type of situation.

Regards, Nagu

+4
source share
8 answers

There is no concept of session variables in window forms. What you can do is:

  • Create an inner class that contains the username and password, as well as any other variables and enumerations necessary for the application (something like Common.cs). They can be accessed through the public properties of the application.

  • Have a parameterized constructor for all forms and submit username and password whenever you show the form.

+5
source
public class MyForm : Form { private string userName; private string password; } 

Since Windows forms are statefull (versus stateless for web forms), you can simply use the field in your Form class.

+3
source

In winforms, you can use variables that are exposed to other forms using methods or properties.

You can also use static variables .

+1
source

In the following example, you will have a controller for each window or group of windows. The controllers will be transferred to each other depending on how they need to cooperate (what knowledge they need to share, etc.). It is important to keep your application in controllers and restrict windows to handling input and user events.

 // pseudocode, because I do not know WinForms that much class MainController { private Guid securityToken; public Guid SecurityToken { get { return securityToken; } set { securityToken = value; } } } class LoginWindowController { MainController mainController; LoginWindow loginWindow; public LoginWindowController(MainController mainController) { this.loginWindow = new LoginWindow(this); this.mainController = mainController; } public void Show() { loginWindow.IsVisible = true; } public void HandleLogin() { Guid token = myobject.Authenticate(loginWindow.Username, loginWindow.Password); if (token != Guid.Empty) { mainController.SecurityToken = token; } } } 
+1
source

You need to think more about scope than a session; as long as the object remains in scope, you can extract values โ€‹โ€‹from your public properties / fields.

In your case, it makes sense to store user data in a static class:

 public static class LoginInfo { public static string UserID; } 

Now you can access the UserID just from anywhere in your code:

 MessageBox.Show(LogInfo.UserID); 
+1
source

In response to your comment on my first answer:

An instance of the new login form is created. How it should have values. This is a login form and therefore I believe that you close it when the user enters the username and password and clicks OK or something else.

Then you cannot get the values โ€‹โ€‹from the login form because it is closed. If you need to take this approach, this could be a way:

  • Do not close the login form, just hide it.
  • Submit the current instance to the following form. Like this: In the login form:

    NextForm nxt = new NextForm (this);

The NextForm constructor will look like this:

 public NextForm(LoginForm frm){ // Code here } 

Now in NextForm you can access properties through "frm".

0
source

from the program that I used with the login form to store global variables and to store the password as a secure string. Within the program, I can โ€œrunโ€ as a โ€œspecific user when I call processes. You can use it for things other than process.start.

 //to run process as another user //create these global variables on the first //form or piece of code in your program class usernameGlobalVariable { public static string var = ""; } class passwordGlobalVariable { public static SecureString var; } // use these as event handlers for text fields //for your login form private void usernameTextBox_TextChanged(object sender, EventArgs e) { usernameGlobalVariable.var = usernameTextBox.Text; } private void passwordTextBox_TextChanged(object sender, EventArgs e) { SecureString passWord = new SecureString(); foreach (char c in passwordTextBox.Text.ToCharArray()) { passWord.AppendChar(c); } passwordGlobalVariable.var = passWord; } //put this on form that launches program //this assigns variables for process.start //change fileName to path and name of program // use \\ in paths string fileName = "c:\\hdatools\\Ping2.exe"; string arguments = ""; string domain = "domain"; //start the process //put this on the page along w the above variables that //launches the app as another user //the .var variables are global { Process.Start( fileName, arguments, usernameGlobalVariable.var, passwordGlobalVariable.var, domain); } 
-1
source

I donโ€™t understand if you are talking about a web application or a stand for an application based on one of your answers. If you are talking about a web application, you can use the Session properties in the Page object.

He set such variables as:

 Session["username"] = "Username"; Session["fullname"] = "User full name"; 

Then you can access, for example:

 lblGreetings.Text = "Hi " + Session["fullname"]; 

Is that what you were after?

-3
source

All Articles