本文介绍了我正在尝试制作练习用户名和密码验证表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较输入的用户名和密码,两个数组用于用户名,一个用于密码。这没用。此外,我尝试过使用字典,但没有运气。



I want to compare the username and password entered up against two arrays one for the username and one for the password. That hasn't work. Also, I have tried using a dictionary but no luck with that.

namespace PracticeLogin
{
    public partial class PasswordPracticeForm : Form
    {
       /* Dictionary<string, string> Credentials = new Dictionary<string, string>
        {
            {"joseph", "password1"},
            ("william", "password2"),
        };*/ //initially tried the above to test the input user and pass against

        /* also tried the following
         * var[] userArr = { "joseph", "william" };
         * var[] passArr = {"password1", password2"};
         * */
        static private Authenticator auth = new Authenticator();

        public PasswordPracticeForm()
        {
            InitializeComponent();

            InitializeMyControl(); //see line 53-63

        }



        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {


            var username = txtBoxUserName.ToString();
            var password = txtPassword.ToString();

            var isvalid = auth.ValidateCredentials(username, password);
            if (isvalid)
            {
                MessageBox.Show("You are authenticated!");
            }
            else
                MessageBox.Show("invalid login information!");

        }

        private void InitializeMyControl() // refer to line 25. this is supposed to make each character of the password display as and asterisk. 
            //instead only shows asterisk in first spot then just shows every character of  the pass instead. retrieved this code from 
        //https://msdn.microsoft.com/en-us/library/aa983584(v=vs.71).aspx but it was not clear on where to put it.  
        {
            //set to no text.
            txtPassword.Text = "";
            //set password characters as asterisk
            txtPassword.Text = "*";
            //password length

            txtPassword.MaxLength = 25;

        }


    }
}
class Authenticator
{
    private Dictionary<string, string> Credentials = new Dictionary<string, string>();
    public Authenticator()
    {
        //username and password
        Credentials.Add("joseph", "password1");
        Credentials.Add("williams", "password");
    }

    public bool ValidateCredentials(string username, string password)
    {
        return Credentials.Any(entry => entry.Key == username && entry.Value == password);
    }
}





我的尝试:



我尝试了不同的东西,我发现在谷歌和其他网站上搜索,如MSDN,Stackoverflow,在这里,还有其他几个,但仍然没有运气。



感谢您的帮助。



此外,这与学校工作或任何与工作相关的任务无关。我是一名残疾的海军退伍军人,只是想尝试不同的事情来获得更多的经验。我在C#上学的最后一门课程是在2014年。我需要大约一年的时间才能再学习。



再次感谢你。



What I have tried:

I have tried different things I have found doing searches on google and other sites such as MSDN, Stackoverflow, Here, a few others and still no luck.

Thank you for you help.

Also, this is not related to school work or any job related task. I am a disabled Navy veteran and just trying to think of different things to do to gain more experience. The last course I took on C# was in 2014. It will be about a year before I am able to take any more.

Thank you again.

推荐答案

private void button1_Click(object sender, EventArgs e)
{
    // Use the Text property to access the text the user has entered
    // Converting the control itself to a string (txtBoxUserName.ToString()) returns the
    // type of the conrtol as text, not what the user has entered into it
    var username = txtBoxUserName.Text;
    var password = txtPassword.Text;

    var isvalid = auth.ValidateCredentials(username, password);
    if (isvalid)
    {
        MessageBox.Show("You are authenticated!");
    }
    else
        MessageBox.Show("invalid login information!");
}

private void InitializeMyControl()
{
    // to make a password box set what the password character is
    txtPassword.PasswordChar = '*'; 
    txtPassword.MaxLength = 25;
}


Dictionary<string, string> userData = new Dictionary<string, string>();
userData["joseph"] = "password1";
userData["william"] = "password2";
string userinputName = "joseph";
string userInputPassword = "password1";
if (userData.ContainsKey(userinputName))
    {
    if (userData[userinputName] == userInputPassword)
        {
        Console.WriteLine("OK");
        }
    else
        {
        Console.WriteLine("Failed to match");
        }
    }
else
    {
    Console.WriteLine("Unknown user");
    }



这不是我要做的 - 我可能会创建一个包含名称和(哈希)密码的User类 - 但它应该做你想要什么!


It's not quite what I would do - I'd probably create a User class which contained the name and (hashed) password - but it should do what you want!


这篇关于我正在尝试制作练习用户名和密码验证表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 16:18