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

解决方案 »

  1.   

    Dr.Close();
    放到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;
            }
      

  2.   


    问题是解决了  可是好像还不能跟数据库校验 UserName     Password
      

  3.   

    换一种连接方式
    using (SqlConnection Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);)
    {
    }
      

  4.   


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

  5.   

    感谢 ojlovecd  的细心回复 !!
      

  6.   


    感谢 honghong0929  的热助