如何在Winforms应用程序中维护用户登录详细信息?

时间:2021-07-04 04:11:36

Hi can I'm very new to windows forms. Here I want to maintain state (like session in web applications) in windows forms.

嗨,我可以对Windows窗体很新。在这里,我想在Windows窗体中维护状态(如Web应用程序中的会话)。

Actually i want to store user login details in session. But i think there is no concept of session in winforms. So what is the alternative method to handle this type of situation.

实际上我想在会话中存储用户登录详细信息。但我认为winforms中没有会话概念。那么处理这种情况的替代方法是什么呢?

Regards, Nagu

8 个解决方案

#1


There is no concept of Session variables in windows forms. What you can do is:

Windows窗体中没有Session变量的概念。你能做的是:

  1. Create a internal class that holds the User name and password and any other variables and enumerations needed across the application (Something like Common.cs). These can be accessed through public properties across the application.

    创建一个内部类,其中包含用户名和密码以及整个应用程序所需的任何其他变量和枚举(类似于Common.cs)。这些可以通过整个应用程序的公共属性访问。

  2. Have a parameterized constructor for all the forms and send the user name and the password whenever you are showing the form.

    为所有表单设置参数化构造函数,并在显示表单时发送用户名和密码。

#2


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

Since windows forms are statefull (opposed to stateless for web forms), you can just use a field in your Form class.

由于Windows窗体是statefull(与Web窗体的无状态相对),因此您只需在Form类中使用一个字段即可。

#3


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

在winforms中,您可以使用通过方法或属性向其他表单公开的变量。

You can also use static variables.

您还可以使用静态变量。

#4


In the following example, you would have a controller for each window or group of windows. The controllers would be passed to one another depending on how they need to collaborate (what knowledge they need to share, etc). The important thing is to keep your application state in the controllers and limit the windows to handling user input and 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;
        }   
    }
}

#5


You need to think more in terms of scope than session; as long as an object remains in scope you will be able to pull values from its public properties/fields.

你需要在范围方面考虑更多而不是会话;只要对象保留在范围内,您就可以从其公共属性/字段中提取值。

In your case it would make sense to store the user details in a static class:

在您的情况下,将用户详细信息存储在静态类中是有意义的:

public static class LoginInfo
{
    public static string UserID;
}

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

现在,您只需从代码中的任何位置访问UserID:

MessageBox.Show(LogInfo.UserID);

#6


In reply to your comment to my first reply:

回复你对我的第一个回复的评论:

You are creating the new instance of the Login form. How is that supposed to have values. It is a Login form and hence I believe you will be closing it as the user enters user name and password and clicks OK or whatever.

您正在创建登录表单的新实例。那怎么会有价值。这是一个登录表单,因此我相信您将关闭它,因为用户输入用户名和密码并单击“确定”或其他任何内容。

Then, there is no way you can get the values from the Login form as it is closed. If you need to stick to this approach, this could be a way:

然后,您无法在关闭时从Login表单中获取值。如果你需要坚持这种方法,这可能是一种方式:

  1. Do not close the Login form, just hide it.
  2. 不要关闭登录表单,只需隐藏它。

  3. Pass the current instance to the next form. Like this: In Login form:

    将当前实例传递给下一个表单。像这样:在登录表格中:

    NextForm nxt = new NextForm(this);

    NextForm nxt = new NextForm(this);

The constructor of NextForm will look like:

NextForm的构造函数如下所示:

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

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

现在在NextForm中,您可以通过“frm”访问属性。

#7


from a program i was using with a login form to store global variables and to store the password as a secure string. Within the program I am able to "run as" a specific user when I call processes. You can use it for other things besides process.start.

来自我使用登录表单存储全局变量并将密码存储为安全字符串的程序。在程序中,当我调用进程时,我能够“运行”为特定用户。您可以将它用于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);
}

#8


It's unclear to me whether you are talking about a web application or a stand along application based upon one of your responses. If you are talking about a web application, you can use the Session properties on the Page object.

根据您的一个回复,我不清楚您是在谈论Web应用程序还是应用程序。如果您正在讨论Web应用程序,则可以使用Page对象上的Session属性。

It would set the variables like this:

它会设置这样的变量:

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

You could then access like:

然后您可以访问:

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

Is that what you were after?

这就是你追求的吗?

#1


There is no concept of Session variables in windows forms. What you can do is:

Windows窗体中没有Session变量的概念。你能做的是:

  1. Create a internal class that holds the User name and password and any other variables and enumerations needed across the application (Something like Common.cs). These can be accessed through public properties across the application.

    创建一个内部类,其中包含用户名和密码以及整个应用程序所需的任何其他变量和枚举(类似于Common.cs)。这些可以通过整个应用程序的公共属性访问。

  2. Have a parameterized constructor for all the forms and send the user name and the password whenever you are showing the form.

    为所有表单设置参数化构造函数,并在显示表单时发送用户名和密码。

#2


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

Since windows forms are statefull (opposed to stateless for web forms), you can just use a field in your Form class.

由于Windows窗体是statefull(与Web窗体的无状态相对),因此您只需在Form类中使用一个字段即可。

#3


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

在winforms中,您可以使用通过方法或属性向其他表单公开的变量。

You can also use static variables.

您还可以使用静态变量。

#4


In the following example, you would have a controller for each window or group of windows. The controllers would be passed to one another depending on how they need to collaborate (what knowledge they need to share, etc). The important thing is to keep your application state in the controllers and limit the windows to handling user input and 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;
        }   
    }
}

#5


You need to think more in terms of scope than session; as long as an object remains in scope you will be able to pull values from its public properties/fields.

你需要在范围方面考虑更多而不是会话;只要对象保留在范围内,您就可以从其公共属性/字段中提取值。

In your case it would make sense to store the user details in a static class:

在您的情况下,将用户详细信息存储在静态类中是有意义的:

public static class LoginInfo
{
    public static string UserID;
}

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

现在,您只需从代码中的任何位置访问UserID:

MessageBox.Show(LogInfo.UserID);

#6


In reply to your comment to my first reply:

回复你对我的第一个回复的评论:

You are creating the new instance of the Login form. How is that supposed to have values. It is a Login form and hence I believe you will be closing it as the user enters user name and password and clicks OK or whatever.

您正在创建登录表单的新实例。那怎么会有价值。这是一个登录表单,因此我相信您将关闭它,因为用户输入用户名和密码并单击“确定”或其他任何内容。

Then, there is no way you can get the values from the Login form as it is closed. If you need to stick to this approach, this could be a way:

然后,您无法在关闭时从Login表单中获取值。如果你需要坚持这种方法,这可能是一种方式:

  1. Do not close the Login form, just hide it.
  2. 不要关闭登录表单,只需隐藏它。

  3. Pass the current instance to the next form. Like this: In Login form:

    将当前实例传递给下一个表单。像这样:在登录表格中:

    NextForm nxt = new NextForm(this);

    NextForm nxt = new NextForm(this);

The constructor of NextForm will look like:

NextForm的构造函数如下所示:

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

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

现在在NextForm中,您可以通过“frm”访问属性。

#7


from a program i was using with a login form to store global variables and to store the password as a secure string. Within the program I am able to "run as" a specific user when I call processes. You can use it for other things besides process.start.

来自我使用登录表单存储全局变量并将密码存储为安全字符串的程序。在程序中,当我调用进程时,我能够“运行”为特定用户。您可以将它用于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);
}

#8


It's unclear to me whether you are talking about a web application or a stand along application based upon one of your responses. If you are talking about a web application, you can use the Session properties on the Page object.

根据您的一个回复,我不清楚您是在谈论Web应用程序还是应用程序。如果您正在讨论Web应用程序,则可以使用Page对象上的Session属性。

It would set the variables like this:

它会设置这样的变量:

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

You could then access like:

然后您可以访问:

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

Is that what you were after?

这就是你追求的吗?