I found this site which was describing a very simple user authentication for LINQ to SQL as at codesamplez.com.
My data in the Database looks like this
| id | name | password |
+-----+------+----------+
| 1 | tic | test |
| 2 | tac | test |
| 3 | toe | test |
For some strange reason the data doesnt validate as I expect, when I call
bool b = IsValidUser("tic" , "test");
this returns FALSE,
But any time I pass the same Username and password combination
bool b = IsValidUser("tic" , "tic");
or
bool b = IsValidUser("a" , "a");
or
bool b = IsValidUser("b" , "b");
it returns true!
below is the code which is basically identical to the referenced page.
public bool IsValidUser(string userName, string passWord)
{
DataClasses1DataContext db = new DataClasses1DataContext();
var users = from u in db.Users
where u.name == userName
&& u.password == passWord
select u;
return Enumerable.Count(users) > 0;
}
Update:
The User class:
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Users")]
public partial class User
{
private int _id;
private string _name;
private string _password;
public User()
{
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_id", DbType="Int NOT NULL")]
public int id
{
get
{
return this._id;
}
set
{
if ((this._id != value))
{
this._id = value;
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_name", DbType="NChar(10)")]
public string name
{
get
{
return this._name;
}
set
{
if ((this._name != value))
{
this._name = value;
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_password", DbType="NChar(10)")]
public string password
{
get
{
return this._password;
}
set
{
if ((this._password != value))
{
this._password = value;
}
}
}
}
Thanks everyone, all very helpful info now. It was my own mistake here, the Database didnt have the data I expected in it. I had 2 database servers and the new data hadnt replicated over to the development server.