C# 如何记住登录表单的用户名或密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12539382/
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-09 23:36:40  来源:igfitidea点击:

How to Remember username or password for login form

c#.netauthenticationremember-me

提问by Smith

I have created a sample Login form in C# Windows Form Application and what I want is to add Remember username or password feature to my login form.

我在 C# Windows 窗体应用程序中创建了一个示例登录表单,我想要的是在我的登录表单中添加记住用户名或密码功能。

I just need to provide my username, cause it will remember my password and automatically fill in the password field (Ex. Like in gmail auth).

我只需要提供我的用户名,因为它会记住我的密码并自动填写密码字段(例如在 gmail auth 中)。

How could I achieve that?

我怎么能做到这一点?

This is my Login form app:- http://i50.tinypic.com/2pzxjb7.jpg

这是我的登录表单应用程序:- http://i50.tinypic.com/2pzxjb7.jpg

采纳答案by Dan

I would use .Net's built-in settings API.

我会使用.Net 的内置设置 API

You can define multiple strongly typed settings with customizable scope; user or application. There is an interface for it in Visual Studio. If you open the project properties, you can select the settings tab. If you don't have a settings file in your project, you can click the link here to have Visual Studio create one. Once you have one, you can enter in the names, types, scope, and default values of your application settings.

您可以定义多个具有可自定义范围的强类型设置;用户或应用程序。Visual Studio 中有它的接口。如果打开项目属性,则可以选择设置选项卡。如果您的项目中没有设置文件,您可以单击此处的链接让 Visual Studio 创建一个。一旦有了,您就可以输入应用程序设置的名称、类型、范围和默认值。

After you set all this up, you will have a class called Settingsunder the <ProjectName>.Propertiesnamespace. You can then use the Settings.Defaultstatic property to access the default settings, change their values, and call the Savemethod to persist them.

完成所有这些设置后,您将Settings<ProjectName>.Properties命名空间下调用一个类。然后,您可以使用Settings.Default静态属性访问默认设置,更改它们的值,并调用该Save方法以保留它们。

When you start your app up, the persisted settings are loaded and you could programmatically enter them into your user interface.

当您启动应用程序时,会加载持久设置,您可以通过编程方式将它们输入到用户界面中。

If all of this is over-kill for your scenario, you could just manually read/write to/from a file in a defined location relative to the user's profile folder.

如果所有这些对于您的场景来说都是过度的,您只需手动读取/写入/写入相对于用户配置文件文件夹的定义位置中的文件。

回答by Senthil

you can store it in the registry, or database or in a application settings. But I'm not sure how secure you want.

您可以将其存储在注册表、数据库或应用程序设置中。但我不确定你想要多安全。

http://msdn.microsoft.com/en-us/library/ms973902.aspx

http://msdn.microsoft.com/en-us/library/ms973902.aspx

回答by Lrodriguez84

I solved using that.

我用那个解决了。

In you winforms project right click and go to properties, select settings and add name to store data (eg. userName and passUser)

在您的 winforms 项目中右键单击并转到属性,选择设置并添加名称以存储数据(例如 userName 和 passUser)

So in you login form add a checkbox remember me.

所以在你的登录表单中添加一个复选框记住我。

In you button loggin check if checkbox true, save user and pass

在您按钮登录检查复选框是否为真,保存用户并通过

        if (checkRemember.Checked)
        {
            Properties.Settings.Default.userName = textBoxUserName.Text;
            Properties.Settings.Default.passUser = textBoxUserPass.Text;
            Properties.Settings.Default.Save();
        }

In you load login form add this

在你加载登录表单中添加这个

        if (Properties.Settings.Default.userName != string.Empty)
        {
            textBoxUserName.Text = Properties.Settings.Default.userName;
            textBoxUserPass.Text = Properties.Settings.Default.passUser;
        }

Regards.

问候。

回答by Pooriya Bastani

if (checkRemember.Checked)
{
    Properties.Settings.Default.userName = textBoxUserName.Text;
    Properties.Settings.Default.passUser = textBoxUserPass.Text;
    Properties.Settings.Default.Save();
}

if (Properties.Settings.Default.userName != string.Empty)
{
   textBoxUserName.Text = Properties.Settings.Default.userName;
   textBoxUserPass.Text = Properties.Settings.Default.passUser;
}

回答by Dashrath

You can try with cookie base remember password

您可以尝试使用 cookie base 记住密码

check below reference :

检查以下参考:

https://www.aspforums.net/Threads/130530/Save-Username-and-Password-in-Cookies-using-C-in-ASPNet/

https://www.aspforums.net/Threads/130530/Save-Username-and-Password-in-Cookies-using-C-in-ASPNet/