【发布时间】:2014-12-18 15:00:19
【问题描述】:
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;
}
}
}
代码运行没有错误,但它没有验证用户名和密码,也成功登录错误的用户名和密码。 while循环和bool返回值解析有问题
【问题讨论】:
-
Dr.Close(); return boolReturnValue;应该在while循环之外。 -
检查我的解决方案并告诉我是否对您有帮助。