为下次程序启动存储用户凭据



这里有一个问题。我有一个程序,当它启动一个loginForm对话框弹出。现在在这个loginForm上,我添加了一个memorberme (onthousand mij)按钮。因此,如果用户选择了log's in,关闭程序,然后再次打开它,他的用户凭据将已经填写。

这是我的loginButton的代码。

private void loginButton_Click(object sender, EventArgs e)
{
    try
    {
        var sr = new System.IO.StreamReader("C:\" + inlogNaamTextbox.Text + "\Login.txt");
        gebruikersnaam = sr.ReadLine();
        passwoord = sr.ReadLine();
        sr.Close();
        if (gebruikersnaam == inlogNaamTextbox.Text && passwoord == inlogPasswoordTextbox.Text)
        {
            classUsername.name = inlogNaamTextbox.Text;
            MessageBox.Show("Je bent nu ingelogd!", "Succes!");
            this.Dispose();
        }
        else
        {
            MessageBox.Show("Gebruikersnaam of wachtwoord fout!", "Fout!");
        }
    }
    catch (System.IO.DirectoryNotFoundException ex)
    {
        MessageBox.Show("De gebruiker bestaat niet!", "Fout!");
    }
}

现在我的问题是:

我在网上搜索了关于记住我实现的选项,但他们总是使用SQL数据库等。我只是有一个登录,存储用户名&如代码中所示,在TXT文件中设置密码。

问题是…

我的问题是;我如何实现这个"记住我"复选框?我已经知道我应该使用系统。Net,但是如果没有sql数据库或其他类似的东西,它还能工作吗?

如果您的问题是保存部分,您可以使用3DES加密之类的东西来保存用户名和密码。

您可以使用您当前的代码并添加类似本文中的内容来加密和解密密码:如何在c#中实现三重DES(完整示例)

如果问题只是如何做读取/保存这个?

我将创建一个文本,像这样:
private void CallOnFormLoad()
{
    string[] allLines = File.ReadAllLines(@"C:passwordfile.txt");
    if (allLines.Length > 1)
    {
        // at least one 2 lines:
        inlogNaamTextbox.Text = allLines[0];
        inlogPasswoordTextbox.Text = allLines[1];
    }
}
private void loginButton_Click(object sender, EventArgs e)
{
    try
    {
        var sr = new System.IO.StreamReader("C:\" + inlogNaamTextbox.Text + "\Login.txt");
        gebruikersnaam = sr.ReadLine();
        passwoord = sr.ReadLine();
        sr.Close();
        if (gebruikersnaam == inlogNaamTextbox.Text && passwoord == inlogPasswoordTextbox.Text)
        {
            classUsername.name = inlogNaamTextbox.Text;
            MessageBox.Show("Je bent nu ingelogd!", "Succes!");
            File.WriteAllLines(@"C:passwordfile.txt", string.Format("{0}n{1}", inlogNaamTextbox.Text, inlogPasswoordTextbox.Text))
            // Don't call Dispose!
            // this.Dispose();
        }
        else
        {
            MessageBox.Show("Gebruikersnaam of wachtwoord fout!", "Fout!");
        }
    }
    catch (System.IO.DirectoryNotFoundException ex)
    {
        MessageBox.Show("De gebruiker bestaat niet!", "Fout!");
    }
}

最新更新