简体   繁体   中英

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. 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:

<asp:Login OnAuthenticate="AuthenticateEventHandler" />

Add event handler in .cs file like this

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.

Here are the reference links:

  1. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.login.authenticate.aspx
  2. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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