Wednesday, 3 April 2013

Login Control in ASP.Net

Login Control in ASP.Net


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>

      private bool AuthenticateUser(string usernamestring 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 senderAuthenticateEventArgs e)
    {
        bool checkAuthentication = false;
        /// call AuthenticateUser method to validate user 
        checkAuthentication = AuthenticateUser(Login1.UserNameLogin1.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

No comments:

Post a Comment