Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.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# asp.net数据库登录功能_C#_Sql_Asp.net - Fatal编程技术网

C# asp.net数据库登录功能

C# asp.net数据库登录功能,c#,sql,asp.net,C#,Sql,Asp.net,你好,我想在asp.net中创建一个登录功能,但我不知道该怎么做。我已经把登录工具箱放好了,接下来该怎么办 void Fillcombo() { string constring = "datasource=localhost;username=username;password=password"; string Query = "select * from hotel"; MySqlConnection conDataBase = new

你好,我想在asp.net中创建一个登录功能,但我不知道该怎么做。我已经把登录工具箱放好了,接下来该怎么办

  void Fillcombo() {
        string constring = "datasource=localhost;username=username;password=password";
        string Query = "select * from hotel";
        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
        MySqlDataReader myReader;
        try
这是登录名

 protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {

    }

我将用一个简单的数据库例程来构建Nasir的早期响应

我使用SQL Server编写了这篇文章,因此可能需要一些翻译。我已经写了这个基于保存的密码已经散列哈希是您的责任

private bool IsAuthenticate(string username,string password) {
    string constring = "datasource=localhost;username=username;password=password";
    string Query = "SELECT Count(*) FROM hotel WHERE (UserLogin = @UserLogin) AND (UserPassword = @UserPassword)";
    int RecordsMatched;

    string UserLogin = username;
    string UserPassword = SomeHashFunction(password)

    using (SqlConnection conDataBase = new SqlConnection(constring)) {
        using (SqlCommand cmd = new SqlCommand(Query, conDataBase)) {
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@UserLogin", UserLogin);
            cmd.Parameters.AddWithValue("@UserPassword", UserPassword);

            try {
                conDataBase.Open();
                RecordsMatched = (int)cmd.ExecuteScalar();
            }
            catch (Exception ex) {
                RecordsMatched = -1;
                Console.Write(ex);
                // your error handling here
            }
            finally {
                conDataBase.Close();
                // your cleanup here
            }
        }
    }
    // Logic for RecordsMatched
    // -1: An error occurred
    //  0: UserLogin and/or UserPassword did not match
    //  1: Authenticated
    // 2+: You have multiple users with same credentials

    return (RecordsMatched == 1);
}

首先连接到数据库。简单地从工具箱中拖放并不能创建功能。我在void Fillcombo()中有它{string constring=“datasource=localhost;username=username;password=password”;string Query=“select*from hotel”;MySqlConnection conDataBase=new MySqlConnection(constring);MySqlCommand cmdDataBase=new MySqlCommand(Query,conDataBase);MySqlDataReader myReader;try您可以将该代码添加到原始问题中,使其适当格式化吗?
private bool IsAuthenticate(string username,string password) {
    string constring = "datasource=localhost;username=username;password=password";
    string Query = "SELECT Count(*) FROM hotel WHERE (UserLogin = @UserLogin) AND (UserPassword = @UserPassword)";
    int RecordsMatched;

    string UserLogin = username;
    string UserPassword = SomeHashFunction(password)

    using (SqlConnection conDataBase = new SqlConnection(constring)) {
        using (SqlCommand cmd = new SqlCommand(Query, conDataBase)) {
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@UserLogin", UserLogin);
            cmd.Parameters.AddWithValue("@UserPassword", UserPassword);

            try {
                conDataBase.Open();
                RecordsMatched = (int)cmd.ExecuteScalar();
            }
            catch (Exception ex) {
                RecordsMatched = -1;
                Console.Write(ex);
                // your error handling here
            }
            finally {
                conDataBase.Close();
                // your cleanup here
            }
        }
    }
    // Logic for RecordsMatched
    // -1: An error occurred
    //  0: UserLogin and/or UserPassword did not match
    //  1: Authenticated
    // 2+: You have multiple users with same credentials

    return (RecordsMatched == 1);
}