LINQ - login page - Asked By Lavinia Rayan on 09-Jan-12 10:34 PM

hi,
how to use linq query for username, password checking in login page?with example
Reena Jain replied to Lavinia Rayan on 09-Jan-12 10:38 PM
hi,

This leaves you with the check for the status, if the username/password combo was correct then you have the user record returned and you can check the status from that.

User user = (from u in db.Users
   where u.Username.Equals(username) &&  
       u.Password.Equals(UserSecurity.GetPasswordHash(username, password))
   select u).FirstOrDefault();
 
if (user == null)
{
  // Invalid user name or password
}
else if (user.Status != true)
{
  // User inactive
}
else
{
  // Success
}

If you must indicate the difference between invalid user name vs invalid password you can do the following.

User user = (from u in db.Users
   where u.Username.Equals(username)
   select u).FirstOrDefault();
 
if  (user == null)
{
  // Invalid user name
}
else if (!user.password.Equals(...))
{
  // Invalid password
}
else if (user.Status != true)
{
  // User inactive
}
else
{
  // Success
}
Jitendra Faye replied to Lavinia Rayan on 09-Jan-12 10:40 PM
Use this code-


Use this code-

var result = from u in context.Users
        where u.UserName== TxtUser.Text && u.PassWord== TxtPassword.Text
        select u;

if(return users.Count()>0)
 {
    //Valid
  }
else
{
  //InValid
}


Try this and let me know.
 

Jitendra Faye replied to Lavinia Rayan on 09-Jan-12 10:41 PM
Change like this-

var result = from u in context.Users
      where u.UserName== TxtUser.Text && u.PassWord== TxtPassword.Text
      select u;

if(result users.Count()>0)
 {
    //Valid
  }
else
{
  //InValid
}


Try this and let me know.

Lavinia Rayan replied to Jitendra Faye on 09-Jan-12 10:52 PM
hi,
whay u mean by context.users?Is it any classname?
Jitendra Faye replied to Lavinia Rayan on 09-Jan-12 11:37 PM
Here=

var result = from u in context.Users
    where u.UserName== TxtUser.Text && u.PassWord== TxtPassword.Text
    select u;



context= name of DataContext
Users= Name of table name