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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1186450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:18:59  来源:igfitidea点击:

How do I maintain user login details in a Winforms application?

c#winforms

提问by Nagu

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

问候, 纳古

采纳答案by danish

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

Windows 窗体中没有会话变量的概念。你可以做的是:

  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.

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

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

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

回答by rahul

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

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

You can also use static variables.

您还可以使用静态变量

回答by Paul van Brenk

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 窗体是有状态的(与 Web 窗体的无状态相反),您可以只使用 Form 类中的字段。

回答by tsimon

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?

那是你追求的吗?

回答by danish

In reply to your comment to my first reply:

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

You are creating the newinstance 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:

然后,您无法从关闭的登录表单中获取值。如果您需要坚持这种方法,这可能是一种方法:

  1. Do not close the Login form, just hide it.
  2. Pass the current instance to the next form. Like this: In Login form:

    NextForm nxt = new NextForm(this);

  1. 不要关闭登录表单,只需将其隐藏即可。
  2. 将当前实例传递给下一个表单。像这样: 在登录表单中:

    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”访问属性。

回答by danish

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;
        }   
    }
}

回答by Phil Gan

You need to think more in terms of scopethan 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);

回答by superKing

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);
}