0

我正在使用表单身份验证。在这里,我使用登录控件,在 (login.cs) 控件中,我正在调用以下方法来验证用户。在这里,我使用本地名称作为用户名和密码,现在我想连接到数据库,以便检索用户名和密码。

private bool Authenticateme(string username, string password, bool remberusername)
{
    String localusername = "username";
    String localpassword = "amar";
    if (username.Equals(localusername) && password.Equals(localpassword))
    {
        return true;
    }
    else
    {
        return false;
    }
}
4

3 回答 3

2

您可以使用MemberShip 和 Role Providers

于 2012-08-04T06:23:19.187 回答
0

您需要OnAuthenticate像这样添加它来实现:

<asp:Login OnAuthenticate="AuthenticateEventHandler" />

像这样在 .cs 文件中添加事件处理程序

private void OnAuthenticate(object sender, AuthenticateEventArgs e)
{
    bool Authenticated = false;
    Authenticated = SiteSpecificAuthenticationMethod(Login1.UserName, Login1.Password);

    e.Authenticated = Authenticated;
}

实现SiteSpecificAuthenticationMethod将根据数据库验证用户。

以下是参考链接:

  1. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.login.authenticate.aspx
  2. http://forums.asp.net/t/1403132.aspx

如果您需要更多帮助,请告诉我。

于 2012-08-04T06:25:58.517 回答
-1

尝试以下操作并使用字段用户名和密码创建 SQL Server 数据库

SQlConnection con = new SqlConnection("Database connection string");
const string uName="UserName";
const string pWord="Password";
public bool getAuthenticate(string UserName,string Password)
{
    con.Open();

    SqlCommand cmd = new SqlCommand(con);
    SQlParameter[] param=new SqlParameter[]{
         new SqlParamter(uName,UserName),
         new SqlParameter(pWord,Password)
    };
    cmd.Parameters.AddRanage(param);
    SqlDataReader rd;
    rd = cmd.executeReader();

    if(rd.read())
    {
         con.Close();
         return true;
    }
    else
    {
         con.Close();
         return false;
    }
}
于 2012-08-04T06:50:51.550 回答