There are many times comes when a web application having a login page to authenticate user. User can create own login control but visual studio also enables this option to create login control for saving time. There we have used built-in feature in visual studio to create login page with help of by using default login control.
Let’s see the brief demonstration of using default login control.
Step 1: first open visual studio and select new project from file menu and then click on website.
Step 2: After successful creating new project then click on toolbox from left pane, drag login control from left pane and drop it on your web page.
Step 3: after dropping it on your web page it will appear as follows
Step 4: If you want to change formatting of login control then click login task button and select Auto Formatting
After selecting auto formatting option from login task then another formatting window appears where you have more choice to select login control formatting.
After choosing the appropriate formatting then you can set properties of login control such as error messages, login control header text etc.
Now to validate user double click on login control and write the following code on Login1_Authenticate1 event
/// <summary>
/// To check login user is authenticated or not
/// </summary>
private bool AuthenticateUser(string username, string password)
{
bool boolcheckReturn = false;
SqlConnection con = new SqlConnection(ConnectionString.ConnString());
SqlCommand cmd = new SqlCommand("select username from tbllogin where username ='"+username+"' and password ='"+password+"'", con);
//// use to explore Sqlcommand used text query
cmd.CommandType = System.Data.CommandType.Text;
/// connection open
con.Open();
/// ExecuteScalar() method reaturn object if there record found then return first row of first cell value
object obj = cmd.ExecuteScalar();
/// check user is authenticate or not
if (obj != null)
{
/// return true if user authenticate
return boolcheckReturn = true;
}
else
{
/// return false if user are not authenticate
return boolcheckReturn = false;
}
}
/// <summary>
/// authenticate event when click on login button
/// </summary>
protected void Login1_Authenticate1(object sender, AuthenticateEventArgs e)
{
bool checkAuthentication = false;
/// call AuthenticateUser method to validate user
checkAuthentication = AuthenticateUser(Login1.UserName, Login1.Password);
e.Authenticated = checkAuthentication;
if (checkAuthentication)
{
// successful login with full authentication
/// if user are authenticated then anoter home.aspx page are opened
Response.Redirect("home.aspx");
}
}
After run the program if you click on Log In button then following required field validator are displays.
If you enter wrong Username or Password then following message are displays.
No comments:
Post a Comment