articles

Home / DeveloperSection / Articles / Login Control in ASP.Net

Login Control in ASP.Net

Anonymous User13121 28-Aug-2011

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.

Login Control in ASP.Net

Step 3: after dropping it on your web page it will appear as follows

Login Control in ASP.Net

Step 4: If you want to change formatting of login control then click login task button and select Auto Formatting

Login Control in ASP.Net

Login Control in ASP.Net

After selecting auto formatting option from login task then another formatting window appears where you have more choice to select login control formatting.

Login Control in ASP.Net

After choosing the appropriate formatting then you can set properties of login control such as error messages, login control header text etc. 

Login Control in ASP.Net

Login Control in ASP.Net

Login Control in ASP.Net

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>
 
      privateboolAuthenticateUser(stringusername, stringpassword)
    {
        boolboolcheckReturn=false;
        SqlConnectioncon= new SqlConnection(ConnectionString.ConnString());
        SqlCommandcmd=newSqlCommand("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
        objectobj=cmd.ExecuteScalar();
 
        /// check user is authenticate or not
        if (obj!=null)
        {
            /// return true if user authenticate
            returnboolcheckReturn=true;
        }
        else
        {
            /// return false if user are not  authenticate
            returnboolcheckReturn=false;
        }
       
 
 
    }
    ///<summary>
    ///   authenticate event when click on login button 
    ///</summary>
  
    protectedvoidLogin1_Authenticate1(objectsender, AuthenticateEventArgse)
    {
        boolcheckAuthentication=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.

Login Control in ASP.Net

If you enter wrong Username or Password then following message are displays.

Login Control in ASP.Net

 


Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By