0
var demo=context.UserDetails.SingleOrDefault(x=>x.UserName==UserName && x.Password==Password);

Im getting out puts even case do not match

for eg: UserName=Demo Password=Test Loged in as UserId=1.

UserName=dEmo Password=test Loged in as UserId=1 Please help me to make it case sensitive

Krishna shidnekoppa
  • 1,011
  • 13
  • 20
  • As an advice, don't store passwords as a text in your database. Use hashing. http://blogs.msdn.com/b/csharpfaq/archive/2006/10/09/how-do-i-calculate-a-md5-hash-from-a-string_3f00_.aspx – Kemal Kefeli Nov 14 '15 at 23:18
  • The duplicate and some of the answers say all there is to say about this. – Gert Arnold Nov 15 '15 at 09:19

2 Answers2

1

That's because you are using LINQ To Entities which is ultimately convert your Lambda Expressions into SQL statements. That means the case sensitivity is at the mercy of your SQL Server which by default has SQL_Latin1_General_CP1_CI_AS Collation and that is NOT case sensitive.

Using ObjectQuery.ToTraceString to see the generated SQL query that has been actually submitted to SQL Server reveals the mystery:

string sqlQuery = ((ObjectQuery)context.Thingies
        .Where(t => t.Name == "ThingamaBob")).ToTraceString();

Your Example-

var demo=((ObjectQuery)context.UserDetails.SingleOrDefault(x=>x.UserName==UserName && x.Password==Password)).ToTraceString();

Please find here for more details

Community
  • 1
  • 1
Teju MB
  • 1,333
  • 5
  • 20
  • 37
  • Let's assume the collation is case sensitive, and there are many nvarchar indexes that the system needs to search on. Users may enter mixed case texts from the UI. What do you recommend then? In my opinion this is not a solution to ops issue. – Kosala W Nov 15 '15 at 03:25
-1

Use Equals;

 var demo = context.UserDetails.SingleOrDefault(x => x.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase) && x.Password.Equals(Password, StringComparison.CurrentCulture);
Kosala W
  • 2,133
  • 1
  • 15
  • 20
  • I cannot recommend plain text passwords. But you do not have to worry about database collation if you are using this method. What we are doing here is a string comparison which happens at your data layer. If the db collation supports case sensitivity, then the LINQ to SQL translated query will not return anything back to your data layer when upper/lower case texts do not match. But if the collation does not support case sensitivity, then you will get a result from the SQL query even though the cases do not match. That's where the "Equals" operator comes in to play. It takes care of them. – Kosala W Nov 15 '15 at 02:55