Linq makes case-insensitive comparisons

I want a user object only with the exact password and matches with it. However, this query retrieves the result, even if the password for the password is not the same:

db.Users.Where(u => u.Username.ToLower() == username.ToLower() &&
                        u.Password == password).FirstOrDefault();

What am I missing?

+5
source share
7 answers

The easiest way is to match the username in the database under its case-insensitve rules and match the passwords in .NET in accordance with its case-sensitive rules:

db.Users.Where(u => u.Username == username).ToList().Where(u => u.Password == password).FirstOrDefault();

ToList() moves from LINQ based on a database based on LINQ objects, and since there will only be one suitable case in any case, the performance impact is negligible.

The problem with saving the password in the database though!

+7

, ( TSQL). ! - ( ). ( blob) , .

+3

Try:

db.Users.Where(u => string.Compare(u.Username, username, System.StringComparison.OrdinalIngoreCase) == 0 &&
                    string.Compare(u.Password, password) == 0).FirstOrDefault();

SQL, .NET/Linq.

+2

Password Users, , ​​ Latin1_General_CS_AS.

, , (, SHA1-) , .

+1

u.Password password , .

0
source

try changing the column as follows:

alter table [Users] alter column [password] nvarchar(100) collate Chinese_PRC_CS_AI
0
source

All Articles