简体   繁体   中英

VS2015 C#, How do I get the login form to reference usernames and passwords for authentication

Newbie coder here.

I want to make an application which is a simple windows form which is a login window. Using array to reference the login username and password.

So far this is what I've gotten. And seem to have encountered an error with the code. I can't seem to figure it out.

The code gives me an error saying that Represents a boolean (true or false) value.

Image of the code: http://i.stack.imgur.com/uvmtP.png

private void btnLogin_Click(object sender, EventArgs e)
    {
        string[] Username = { "user1", "user2", "user3" };
        string[] Password = { "Password1", "Password2", "Password3" };

        if (Username[0].ToString() == Password[0])

        this.Close();
        th = new Thread(opennewform);
        th.SetApartmentState(ApartmentState.STA);
        th.Start();

        if ((txtPasswd.Text == Username) && (txtUser.Text == Password))
        {
            Success_Login Success = new Success_Login();
            Success.Show();
        }
        else
            MessageBox.Show("Enter valid username and/or password");
    }
}

Many thanks.

Username and Password are string arrays you need to use Array.Contains or Array.IndexOf to check if TextBox values exists in those arrays.

if(Array.Contains(Username, txtUser.Text) && Array.Contains(Password, Password.Text))
{

}

Or use IndexOf

if(Array.IndexOf(Username, txtUser.Text) != -1 && Array.IndexOf(Password, txtPassword.Text)!= -1)
{

}

wow what you try to do?

i see 2 error first one

you try compare your username text with the password for validation it make no sence

then you try to evaluate an equality between an array of string and a simple string.

so correct this first for get answer

to me you

try to do something like

if(username[0] == txtuser.text && Password[0] == txtpassword.text)
{
 // then your first user can login
}

but its not how we do a login in c# password can't be stored in code since the code can be read easily.

I think you need a for loop to check for each username and password. Let me write a stupid simple program but workable of login process.

My output:

User=user1, Password=abc login failed!!
User=user2, Password=xxx login failed!!
User user2 login success!!
User user1 login success!!

My source code:

using System;

namespace sam_StreamReader
{
    class Program
    {
        static void Main(string[] args)
        {
            login("user1", "abc");
            login("user2", "xxx");
            login("user2", "Password2");
            login("user1", "Password1");
        }
        static bool login(string p_user_name,string p_password)
        {
            String[] Username = { "user1", "user2", "user3" };
            String[] Password = { "Password1", "Password2", "Password3" };
            for(int i=0;i<Username.Length;i++)
            {
                if(p_user_name == Username[i])
                {
                    if(p_password == Password[i])
                    {
                        System.Console.WriteLine("User "+p_user_name+" login success!!");
                        return true;
                    }
                }
            }
            System.Console.WriteLine("User=" + p_user_name + ", Password="+p_password+" login failed!!");
            return false;
        }

    }
}

Hope this helps~

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM