Asp.net mvc 3 检查用户名&;在mvc中使用表单身份验证的密码?

Asp.net mvc 3 检查用户名&;在mvc中使用表单身份验证的密码?,asp.net-mvc-3,forms-authentication,Asp.net Mvc 3,Forms Authentication,我正在尝试编写一个方法来验证web应用程序中的用户。代码如下: public void SignIn(UserInfo Entity) { if(this.CheckUser(Entity.UserName, Entity.Password)) { FormsAuthentication.RedirectFromLoginPage(Entity.UserName, false); } else

我正在尝试编写一个方法来验证web应用程序中的用户。代码如下:

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;
            }

        }

    }
public void登录(UserInfo实体)
{
if(this.CheckUser(Entity.UserName,Entity.Password))
{
FormsAuthentication.RedirectFromLoginPage(Entity.UserName,false);
}
其他的
{
}
}
public bool CheckUser(字符串\u用户名,字符串\u密码)
{
使用(var context=new tourblogenties1())
{
列表测试=null;
test=(来自context.UserInfoes中的s)
其中s.UserName==\u用户名和s.Password==\u密码
选择s.ToList();
if(test==null)
{
返回false;
}
其他的
{
返回true;
}
}
}

问题是我可以使用任何用户名和密码登录,而这些用户名和密码实际上并没有注册。我做错了什么?提前感谢。

您的列表从不为空。您需要检查列表是否包含元素

我建议您使用
Any()
Count()
方法

Any()-确定序列是否包含任何元素

Count()-返回一个数字,该数字表示指定序列中满足条件的元素数

样品 任何() 计数() 更多信息

很抱歉未能及时标记为正确,谢谢您的帮助。
return (from s in context.UserInfoes
       where s.UserName == _UserName && s.Password == _Password
       select s).Any();
return (from s in context.UserInfoes
       where s.UserName == _UserName && s.Password == _Password
       select s).Count() != 0;