I’m trying to write a method that authenticate a user in the web apps. Here is the code:
public void SignIn(UserInfo Entity)
{
if(this.CheckUser(Entity.UserName, Entity.Password))
{
FormsAuthentication.RedirectFromLoginPage(Entity.UserName, false);
}
else
{
}
}
public bool CheckUser(String _UserName, string _Password)
{
using (var context = new TourBlogEntities1())
{
List<UserInfo> test = null;
test = (from s in context.UserInfoes
where s.UserName == _UserName && s.Password == _Password
select s).ToList<UserInfo>();
if(test==null)
{
return false;
}
else
{
return true;
}
}
}
The problem is I can sign in with any username & password, which are not actually registered. What I have done wrong? Thanks in advance.
Your list is never null. You need to check whether your list contains a element.
I suggest you use the
Any()orCount()method.Sample
Any()
Count()
More Information