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;
}
}
您可以使用MemberShip和角色提供程序
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:
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.