[英]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.我想制作一个应用程序,它是一个简单的 Windows 窗体,它是一个登录窗口。 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
代码图片: 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.用户名和密码是字符串数组,您需要使用Array.Contains或Array.IndexOf来检查这些数组中是否存在 TextBox 值。
if(Array.Contains(Username, txtUser.Text) && Array.Contains(Password, Password.Text))
{
}
Or use IndexOf
或者使用
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我首先看到 2 个错误
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.但它不是我们如何在 c# 中登录密码不能存储在代码中,因为代码可以很容易地阅读。
I think you need a for loop to check for each username and password.我认为您需要一个 for 循环来检查每个用户名和密码。 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~希望这有帮助~
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.