Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » Visual Studio 2005 » How to use Login Control in Visual Studio 2005

How to use Login Control in Visual Studio 2005

In the recent version of Visual Studio 2005, Microsoft has taken care of a very common functionality of the web applications. In the following article we will see that how to use login control using C#.Net.

Author Rank :
Page Views : 30332
Downloads : 469
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
WebSite2.zip
 
 
Nevron Gauge for SharePoint
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

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.
 
Steps to use the sample:-

  1. Download a zip file --> Unzip the file
  2. Go to Start-->RUN-->inetmgr.exe
  3. Go to Websites-->Default web sites
  4. Right click on it-->NewVirtual Directory-->it will start the wizard
  5. Map the unzip folder with the virtual directory-->finish
  6. Run the application/Open the solution file.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Sushmita Kumari
Sushmita kumari holds  Computer Science degree.She is Brain Bench and Microsoft certified professional. Presently she is working  as a software engineer. She has good hands on experience with .NET technologies. Other than .NET, she has worked with Microsoft Commerce Server, and Microsoft Content Management server.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
Login Unsuccessful by asp On April 11, 2007

Hi i try the code u post, i dont get any error but i failed to login thru the login control it says

"Your login attempt was not successful. Please try again." But the id & password are correct...

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("Default3.aspx");  }

}

private bool SiteLevelCustomAuthenticationMethod(string UserName, string Password)

{

   bool boolReturnValue = false;

   SqlConnection Connection = new SqlConnection(@"server=SQLEXPRESS; database=Test; uid=a; pwd=a;");

   String strSQL = "Select * From tblLogin";

   SqlCommand command = new SqlCommand(strSQL, Connection);

   Connection.Open();

   SqlDataReader Dr = command.ExecuteReader();

   while (Dr.Read())

   {

         if ((UserName == Dr["id"].ToString()) & (Password == Dr["pswd"].ToString()))

         boolReturnValue = true;

   }

  Dr.Close();

  return boolReturnValue;

}

Please help me....

Thank You in advance

Reply | Email | Modify 
login successful by jasmine On July 13, 2007
Hi i try this code. It build successfully. but when running it throws exception like this. (Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.) I don't know how to tackle this. Will anyone help me?
Reply | Email | Modify 
REGISTERING by Gaopalelwe On October 6, 2009

Hi id like to know if you have a tutorial which shows how to make a new member to register and also for the site to remember how many times the person has visited the site? 

Reply | Email | Modify 
Hello by Nithin On July 28, 2010
Reply | Email | Modify 
Error in code by Abhinav On December 18, 2010
This code works for only first entry in database. By returning bool value outside the while loop it is working fine
Reply | Email | Modify 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.