asp.net登录功能到数据库

时间:2017-05-04 16:03:52

标签: c# sql asp.net

您好我想在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)
    {

    }

2 个答案:

答案 0 :(得分:0)

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
    string userName = Login1.UserName;
    string password = Login1.Password;

    bool result = IsAuthenticate(userName, password);
    if ((result))
    {
        e.Authenticated = true;
    }
    else
    {
        e.Authenticated = false;
    }

}

protected bool IsAuthenticate(string username,string password)
{
    // implement this method according to your database..

}

答案 1 :(得分:0)

我将使用简单的数据库例程构建 Nasir的早期响应。

我使用SQL Server编写了这个,因此可能需要一些翻译。我已根据保存的密码编写了这个已被哈希的。 Hashing是您的责任!

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