Chintan Gandhi Chintan Gandhi - 3 months ago 17
ASP.NET (C#) Question

How to check case sensitivity of username when login to a site in MVC 5 using LINQ

I Need Help to check Case Sensitivity of LoginId & Password Like Admin and Password : SsA.123 but currently it takes admin as LoginId and ssa.123 as Passwod.

public static class LoginValidation
{
//VALIDATE LOGIN AND GET USER NAME AND USER ID FOR FUTURE USE
public static bool Validate_Login(string first_input, string password, DBEntities db, out string Name, out int id)
{

Name = db.MAST_User.Where(x => x.LoginId == first_input).Select(x => x.UserName).FirstOrDefault();//GET USER NAME TO DISPLAY AFTER LOGIN

id = db.MAST_User.Where(x => x.LoginId == first_input).Select(x => x.MastId).FirstOrDefault(); //GET USER ID TO CHECK RIGHTS

return db.MAST_User.First(x => x.LoginId == first_input).Password == password ? true : false; //RETURNS BOOLEAN VALUE TRUE OR FALSE
}

}

Kaf Kaf
Answer

Your comparison query is executing within the database server and it may have set to be case insensitive (for example SQL server is case insensitive by default).

Easiest way would be to bring your query results to memory by converting it to an Array()/List()/Variable and then compare within C# code. Ex;

//Load into memory
var users = db.MAST_User.Where(x => x.LoginId == first_input).ToArray(); 

//Compare in C#
if(users.Any(u=> u.LoginId == first_input && u.Password == password))
{
    return true;
}
return false;

On a separate note, please check your FirstOrDefault() object for null before assigning its properties to other variables.

Comments