Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法验证用户名和密码,while循环中出现错误_C#_.net - Fatal编程技术网

C# 无法验证用户名和密码,while循环中出现错误

C# 无法验证用户名和密码,while循环中出现错误,c#,.net,C#,.net,} 代码正在运行,但没有验证用户名和密码,也没有成功登录错误的用户名和密码。while循环和bool返回值解析中出现错误您正在关闭while循环中的读卡器,并在第一次迭代while时返回值boolReturnValue。这意味着,如果在循环的第一次迭代中,if check不是true,它将始终返回false using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syst

}


代码正在运行,但没有验证用户名和密码,也没有成功登录错误的用户名和密码。while循环和bool返回值解析中出现错误

您正在关闭while循环中的读卡器,并在第一次迭代
while
时返回值
boolReturnValue
。这意味着,如果在循环的第一次迭代中,
if check
不是true,它将始终返回
false

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
        ViewState["LoginErrors"] = 0;
}

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
    if (YourValidationFunction(Login1.UserName, Login1.Password))
    {
       // e.Authenticated = true;
        Login1.Visible = false;
        MessageLabel.Text = "Successfully Logged In";
    }
    else
    {
        e.Authenticated = false;
    }
}

protected void Login1_LoginError(object sender, EventArgs e)
{
    if (ViewState["LoginErrors"] == null)
        ViewState["LoginErrors"] = 0;

    int ErrorCount = (int)ViewState["LoginErrors"] + 1;
    ViewState["LoginErrors"] = ErrorCount;

    if ((ErrorCount > 3) && (Login1.PasswordRecoveryUrl != string.Empty))
        Response.Redirect(Login1.PasswordRecoveryUrl);
}

private bool YourValidationFunction(string UserName, string Password)
{
    bool boolReturnValue = false;
    string strConnection = "server=example;database=TEST_dw;uid=test;pwd=test;";
    SqlConnection sqlConnection = new SqlConnection(strConnection);
    String SQLQuery = "SELECT UserName, Password FROM Login";
    SqlCommand command = new SqlCommand(SQLQuery, sqlConnection);
    SqlDataReader Dr;
    sqlConnection.Open();
    Dr = command.ExecuteReader();

    while (Dr.Read())
    {

        if ((UserName == Dr["UserName"].ToString()) && (Password ==     Dr["Password"].ToString()))

            {
                boolReturnValue = true;
                break;
            }


        Dr.Close();
        return boolReturnValue;
    }
}
代码应该是这样的。如果使用Debug,您可以很容易地看到这一点。有关调试的本教程对您很有用:

另外,对您来说,更好的方法是查询数据库,查看是否有任何用户使用此密码和用户名,而不是让每个用户都使用此密码和用户名,然后循环并检查是否有。如果你有100000个用户,这将是一个性能问题,只是说

// previous code

while (Dr.Read())
{
    if ((UserName == Dr["UserName"].ToString()) && (Password ==     Dr["Password"].ToString()))
    {
        boolReturnValue = true;
        break;
    }
}

Dr.Close();
return boolReturnValue;
最后一行是tenacy运算符
返回结果!=0 ? 真:假。这意味着

private bool YourValidationFunction(string UserName, string Password)
{

    string strConnection = "server=example;database=TEST_dw;uid=test;pwd=test;";
    SqlConnection sqlConnection = new SqlConnection(strConnection);

    sqlConnection.Open();

    String query = "SELECT Count(*) FROM Login WHERE UserName=@UserName AND Password=@Password";

    SqlCommand command = new SqlCommand(query, sqlConnection);
    command.Parameters.AddWithValue("@UserName", UserName);
    command.Parameters.AddWithValue("@Password", Password);

    int result = Convert.ToInt32(command.ExecuteScalar());

    sqlConnection.Close();

    return result != 0 ? true : false;
}

我建议您以后在其他类中编写数据访问层:下面是我编写示例数据访问层的示例问题:

Dr.Close();返回boolReturnValue
应该在
的while
循环之外。检查我的解决方案并告诉我是否对您有所帮助。这真的很有帮助。。。现在完成了,非常感谢
if(result !=0)
   return true;
else
   return false;