asp.net身份验证和授权

本文关键字:授权 身份验证 net asp | 更新日期: 2024-09-24 16:18:54

我正在使用Form身份验证。在这里,我使用登录控件,在(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;
    }
}

asp.net身份验证和授权

您可以使用MemberShip和Role Providers

您需要通过如下添加来实现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;
}

实现将根据数据库验证用户的CCD_ 2。

以下是参考链接:

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

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

尝试以下操作并使用字段用户名和密码创建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;
    }
}