public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
bool Authenticated = false;
Authenticated = SiteLevelCustomAuthenticationMethod(Login1.UserName, Login1.Password);
e.Authenticated = Authenticated;
if (Authenticated == true)
{
Response.Redirect("Default.aspx");
}
}
private bool SiteLevelCustomAuthenticationMethod(string UserName, string Password)
{
bool boolReturnValue = false;
SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
String strSQL = "Select * From [User]";
SqlCommand command = new SqlCommand(strSQL, Connection);
SqlDataReader Dr;
Connection.Open();
Dr = command.ExecuteReader();
while (Dr.Read()) //阅读器关闭时尝试调用 Read 无效
{
if ((this.Login1.UserName == Dr["UserName"].ToString()) & (this.Login1.Password == Dr["Password"].ToString()))
{
boolReturnValue = false;
Dr.Close();
}
}
return boolReturnValue;
}
}
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
bool Authenticated = false;
Authenticated = SiteLevelCustomAuthenticationMethod(Login1.UserName, Login1.Password);
e.Authenticated = Authenticated;
if (Authenticated == true)
{
Response.Redirect("Default.aspx");
}
}
private bool SiteLevelCustomAuthenticationMethod(string UserName, string Password)
{
bool boolReturnValue = false;
SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
String strSQL = "Select * From [User]";
SqlCommand command = new SqlCommand(strSQL, Connection);
SqlDataReader Dr;
Connection.Open();
Dr = command.ExecuteReader();
while (Dr.Read()) //阅读器关闭时尝试调用 Read 无效
{
if ((this.Login1.UserName == Dr["UserName"].ToString()) & (this.Login1.Password == Dr["Password"].ToString()))
{
boolReturnValue = false;
Dr.Close();
}
}
return boolReturnValue;
}
}
放到while外面 private bool SiteLevelCustomAuthenticationMethod(string UserName, string Password)
{
bool boolReturnValue = false;
SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
String strSQL = "Select * From [User]";
SqlCommand command = new SqlCommand(strSQL, Connection);
SqlDataReader Dr;
Connection.Open();
Dr = command.ExecuteReader();
while (Dr.Read()) //阅读器关闭时尝试调用 Read 无效
{
if ((this.Login1.UserName == Dr["UserName"].ToString()) & (this.Login1.Password == Dr["Password"].ToString()))
{
boolReturnValue = false;
}
}
Dr.Close();
return boolReturnValue;
}
问题是解决了 可是好像还不能跟数据库校验 UserName Password
using (SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);)
{
}
private bool SiteLevelCustomAuthenticationMethod(string UserName, string Password)
{
bool boolReturnValue = false;
SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
String strSQL = "Select * From [User]";
SqlCommand command = new SqlCommand(strSQL, Connection);
SqlDataReader Dr;
Connection.Open();
Dr = command.ExecuteReader();
while (Dr.Read()) //阅读器关闭时尝试调用 Read 无效
{
if ((this.Login1.UserName == Dr["UserName"].ToString()) & (this.Login1.Password == Dr["Password"].ToString()))
{
boolReturnValue = true;//这里应该是true
}
}
Dr.Close();
return boolReturnValue;
}
感谢 honghong0929 的热助