简体   繁体   English

asp.net身份验证和授权

[英]asp.net authentication and authorisation

I am using Form authentication. 我正在使用表单身份验证。 Here I am using a login control and in (login.cs) control I am calling the following method to validate the user. 在这里,我使用登录控件,在(login.cs)控件中,我调用以下方法来验证用户。 Here I am using local name for both username and password and now I want to connect to the database so I can retrieve username and password. 在这里,我使用本地名称作为用户名和密码,现在我想连接到数据库,以便检索用户名和密码。

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

You need to implement OnAuthenticate by adding it like this: 您需要通过添加如下代码来实现OnAuthenticate

<asp:Login OnAuthenticate="AuthenticateEventHandler" />

Add event handler in .cs file like this 像这样在.cs文件中添加事件处理程序

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

    e.Authenticated = Authenticated;
}

implement SiteSpecificAuthenticationMethod that will validate user against database. 实现SiteSpecificAuthenticationMethod ,它将根据数据库验证用户。

Here are the reference links: 以下是参考链接:

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

Let me know if you need more help. 让我知道您是否需要更多帮助。

Try the following and create a SQL Server database with field username and password 请尝试以下操作,并使用字段用户名和密码创建一个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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM