[英]Not case-sensitive save in SQL?
On my login form after i save my content to SQL and if i try to get the information from the database the information passes the evaluation to true even if the information provided is typed both ways - upper case or lower case.Here is my login code,please help me understand.I'am contacting database with Entity Framework.the currUser is a variable where I save the current user information. 将内容保存到SQL之后,在登录表单上,如果我尝试从数据库中获取信息,即使所提供的信息以大写或小写两种方式键入,信息也会通过评估为true,这是我的登录代码,请帮助我理解。我正在使用Entity Framework与数据库联系。currUser是一个变量,用于保存当前用户信息。
try
{
if (!string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(password))
{
var users = from c in context.CustomerTables where c.username == username && c.password == password select c;
List<CustomerTable> table = users.ToList();
if (table.Any())
{
MessageBox.Show("Successfully logged in.\nWelcome " + username + "!", "Welcome", MessageBoxButton.OK, MessageBoxImage.Asterisk);
currUser.username = username;
currUser.password = password;
return true;
}
else
{
MessageBox.Show("Username or password is invalid.", "Error logging in", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
}
else
{
MessageBox.Show("Username and password format is invalid!","Null username or password",MessageBoxButton.OK,MessageBoxImage.Warning);
return false;
}
The simplest fix would be to replace 最简单的解决方法是更换
if (table.Any())
with 同
if (table.Any() && table[0].username == username && table[0].password == password)
The reason why this would work is that string comparison in C# is case-sensitive by default. 之所以可行,是因为C#中的字符串比较默认情况下区分大小写。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.