How to use login control in .net

As a web developer we know that most of the time our application is having a login as well as the forget password kind of requirement.

Now, using visual studio 2005 it’s very easy to design a login page because the inbuilt login tab has been added into the toolbox of VS 2005 editor, which has different types of control related to login functional.

Fig 1.1: New tab for login

Fig 1.2: All the controls of Login Tab

In the following article we will see that how to use login control using C#.Net. The following code will explain that how to authenticate the user against the database.

Step 1:- Drag and drop the login control on the page then the control will look like a login page at design time.

Fig 1.3: Login control at design time

Step 2: Once the UI is ready then will go ahead with the coding part to see how to write the code for this control.

To write the code for this code we need to handle the Login1_Authenticate event.

So, double click on the Login control it will generate the following code:-

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
bool Authenticated = false;
Authenticated = SiteLevelCustomAuthenticationMethod(Login1.UserName, Login1.Password);
e.Authenticated = Authenticated;
if (Authenticated == true)
{
Response.Redirect(“Home.aspx”);
}
}
private bool SiteLevelCustomAuthenticationMethod(string UserName, string Password)
{
bool boolReturnValue = false;
// Insert code that implements a site-specific custom
// authentication method here.
// This example implementation always returns false.
string strConnection = “server=dtpxp-skumari;database=master;uid=sa;pwd=;”;
SqlConnection Connection = new SqlConnection(strConnection);
String strSQL = “Select * From Employee”;
SqlCommand command =new SqlCommand(strSQL, Connection);
SqlDataReader Dr;
Connection.Open();
Dr=command.ExecuteReader();
while (Dr.Read())
{
if ((UserName == Dr[“name”].ToString()) & (Password == Dr[“Password”].ToString()))
{
boolReturnValue = true;
}
Dr.Close();
return boolReturnValue;
}
}

Login control is having a property called FailureText where you can write your own message.

Fig 1.4: Custom Error Message

Once you have passed the correct login credential then you will be redirected to the home page using the DestinationPageUrl property. DestinationPageUrl is the property of login control which is used to redirect the user to desination page after a successful login. If incorrect login credential it will show the message like “Your login attempt was not successful. Please try again”. This is a custom message specified by the user through the FailureText property.

Fig 1.5: Logion Error

About inbuilt validation:- Login control is having a inbuilt validation feature which is available as a property for programmer. When you will drag-drop the control at design time you will see that the username and password textboxes are marked with star (*) sign which means these fields are required fields.

Fig 1.6: Validation

Login control is having some more features like specifying .CSS property, Button style etc.

Comments
14 Responses to “How to use login control in .net”
  1. mutsumi says:

    Thx. This is what i want 😀

  2. John says:

    Hello Ripal Soni,

    First of all, http://www.ripalsoni.wordpress.com is an excellent site for .NET Interview Question Answers

  3. Rima says:

    Hello

    really this is what i need really nice article How to use login control in .net

    Thanks

  4. parandam says:

    Its realy helps to me

    very Nice

    thanks

  5. Really the way the login control was illustrated it would help in a better way to any of the fresher web developers who were not working on asp.net 2.0

    It will really help others

    Very Nice

    Thanks for the Support

  6. Nitin khaira says:

    I like the way you represented the control and well dimensioned in each aspect …….

    Nice job done

  7. kamini says:

    Fine and good

    well done

    Nice person to interact with

    Thanks for the illustration

  8. Vineeta Agarwal says:

    Hi Soni,

    We are using this login control in our website. we are using ASP.Net Configuration wizard to main the users of the site. so can you please tell me how we can handle logout event of the users.

    Thanks
    Vineeta

  9. Kedar says:

    I tried the same code in my project but i get an error as”Error1 ‘MasterPage.SiteLevelCustomAuthenticationMethod(string, string)’: not all code paths return a value”

    and what code to write in the commented area.

  10. Murali says:

    Very Nice
    It is very helpful.

    Thanks

  11. Basavaraja k says:

    hi,
    Really it is a nice article.
    Thanks for sharing..
    By
    Basavaraja k

  12. ASPAK says:

    Hi,,Ripal..

    u gave nice example how to use login in asp.net..but dear sorry to say u u chang u r last few lines plz .. u give here code is:
    while (Dr.Read())
    {
    if ((UserName == Dr[“name”].ToString()) & (Password == Dr[“Password”].ToString()))
    {
    boolReturnValue = true;
    }
    Dr.Close();
    return boolReturnValue;
    }
    }

    but actually the right code is this u try to use first u r code u will be see error there and after that u put mine code instead of it u will correct it my code is this :

    while (Dr.Read())
    {
    if ((id == Dr[“id”].ToString()) & (password == Dr[“password”].ToString()))
    {
    boolReturnValue = true;
    }
    }
    Dr.Close();

    return boolReturnValue;

    }
    }

    u make small error see it .. i knew u will find it ok ..
    again nice site and helping site to .net developor.. byeeee

  13. Matteo says:

    Hi. I’m a new c# developer and I’ve got a question for you. I created the login form and it works so well. I put it in my MasterPage. Now, I would hide the form when a user is logged and display a message of welcome. I tried by using visual studio’s and but it doesn’t work. The Error Message is: “Object reference not set to an instance of an object” and it refers to the code Authenticated = SiteLevelCustomAuthenticationMethod(Login1.UserName, Login1.Password);
    Can you help me?
    Thank you so much

  14. Ashish Agrawal says:

    nice…

Leave a comment