Anuradha Dawkore
Anuradha Dawkore

Reputation: 21

How to match case-sensitive username and password

I am trying to match user name and password using linq in C# using sql server

    //verify the user.
    var UserDetails = 
    (from user in con.ios_Users
    where (user.LOGIN == LogReq.userName && user.PASSWORD == LogReq.password && user.ACTIVE != 0)

    select new
    {
        user.ID,
        user.TYPEDESCR,
        user.USERNAME
    }).ToList();                     

In above code it returns data user data if i am passing username - demo and password - demo123 but in database user name is - DeMo and password is DeMo123 How to make this linq query case sensitive :(

Thanks in advance :)

Upvotes: 1

Views: 1290

Answers (3)

Nguyễn Văn Phong
Nguyễn Văn Phong

Reputation: 14228

You should use String.Equals

//verify the user.
var UserDetails = 
(from user in con.ios_Users
where (string.Equals(user.LOGIN, LogReq.userName, StringComparison.InvariantCultureIgnoreCase) && string.Equals(user.PASSWORD, LogReq.password , StringComparison.InvariantCultureIgnoreCase) && user.ACTIVE != 0)

select new
{
    user.ID,
    user.TYPEDESCR,
    user.USERNAME
}).ToList();

Upvotes: -1

Athanasios Kataras
Athanasios Kataras

Reputation: 26450

It looks like a database collation issue: https://learn.microsoft.com/en-us/sql/relational-databases/collations/collation-and-unicode-support?view=sql-server-ver15

Collations in SQL Server provide sorting rules, case, and accent sensitivity properties for your data. Collations that are used with character data types, such as char and varchar, dictate the code page and corresponding characters that can be represented for that data type.

The above is for MsSql but it applies for most mainstream databases.

If you are using linqToSql, then the linq is turned to a query and executed in the database, so the actual comparison occurs on the SQL side.

So you need to fix collation on your table.

Upvotes: 2

npinti
npinti

Reputation: 52185

String comparison is case sensitive by default in C# (unless you use specific string comparison mechanisms).

Seeing how you say that:

but in database user name is

Assuming you are using some sort of LinQ to SQL, most likely the culprit is the way in which your column is set up. Some DB engines, by default, match strings in a case insensitive manner, which seems to be the case here as well.

Upvotes: 0

Related Questions