User288213138 posted
Hi jsshivalik,
You can use the Login control directly.
The login control provides user interface elements for logging in to a Web site.
More information about login control you can reference this link:
https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.login?view=netframework-4.7.2
I wrote a demo that you can refer to.
The code:
<div>
<asp:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate"></asp:Login>
</div>
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
if (e.Authenticated)
{
Response.Redirect("https://forums.asp.net/t/2156936.aspx");
}
if (ValidateUser(Login1.UserName, Login1.Password))
{
Response.Redirect("https://forums.asp.net/t/2156936.aspx");
}
else
{
e.Authenticated = false;
}
}
private bool ValidateUser(string username,string password)
{
bool status;
string constr = ConfigurationManager.ConnectionStrings["constr527"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
string query = "Select * from Students";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = query;
cmd.Connection = con;
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;
DataSet ds = new DataSet();
adapter.Fill(ds);
string uname;
string pass;
uname = ds.Tables[0].Rows[0]["Name"].ToString();
pass= ds.Tables[0].Rows[0]["Password"].ToString();
Response.Write(uname);
Response.Write(pass);
con.Close();
if(uname== username && pass== password)
{
Session[""]= uname;
status = true;
}
else
{
status = false;
}
return status;
}
The result:

Best regards,
Sam