Login Control in ASP.NET 3.5

The ASP.NET login controls provide a robust login solution for ASP.NET Web applications without requiring programming. By default, login controls integrate with ASP.NET membership and forms authentication to help automate user authentication for a Web site. It provides you with a ready-to-use user interface that queries the user name and password from the user and offers a Log In button for login. It validate user credentials against the membership API and encapsulating the basic froms authentication functionality like redirecting back to the original requested page in a restricted area of you application after the successful login.

The Login control displays a user interface for user authentication. The Login control contains text boxes for the user name and password and a check box that allows users to indicate whether they want the server to store their identity using ASP.NET membership and automatically be authenticated the next time they visit the site.

The Login control has properties for customized display, for customized messages, and for links to other pages where users can change their password or recover a forgotten password. The Login control can be used as a standalone control on a main or home page, or you can use it on a dedicated login page. If you use the Login control with ASP.NET membership, you do not need to write code to perform authentication. However, if you want to create your own authentication logic, you can handle the Login control's Authenticate event and add custom authentication code.

Note - Login controls might not function correctly if the Method of the ASP.NET Web page is changed from POST (the default) to GET.

  1. Start Microsoft Visual Studio 2008
  2. Create a new ASP.NET WebSite, Like this:

  3. Drag and drop Login control on page from ToolBox.


    Figure 1.

Whenever user hits the Log In button, the control automatically validates the user name and password using the membership API function Membership.ValidateUse() and then calls FormAuthentication.redirectFromLoginPage() if the validation was successful. All options on the UI of the LoginControl affect the input delivered by the control to these methods. For Example, if you click the "Remember me next time" check box, it passes the value true to the createPresistentCookie parameter of the RedirectFromLoginPage() method. Therefore, the FormAuthenticateModule creates a persistent cookie.

There are three Login Tasks by default.

  • Auto Format - you can select default schemes.
  • Convert To Template - You can edit content of Login Control.
  • Administer Website - You can configure Web Site Administration Tools, Like Security, Application, Provider. 


Figure 2. 

  1. <form id="form1" runat="server">  
  2. <div>  
  3. <asp:Login ID="Login1" runat="server" BackColor="#F7F7DE" BorderColor="#CCCC99" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="10pt">  
  4. <TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="#FFFFFF" />  
  5. </asp:Login>  
  6. </div>  
  7. </form>  
You can change styles of LoginControl using css too,  Like this:
  1. .LoginControl    
  2. {    
  3.       background-color:#F7F7DE;    
  4.       border-color:#CCCC99;    
  5.       border-style:solid;    
  6.       border-width:1px;    
  7.       font-family:Verdana;    
  8.       font-size:10px;    
  9. }   
And now apply css to control:
  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head runat="server">  
  3.     <title>Login Control</title>  
  4.     <link href="StyleSheet.css" type="text/css" rel="Stylesheet" />  
  5. </head>  
  6. <body>  
  7.     <form id="form1" runat="server">  
  8.     <div>  
  9.         <asp:Login ID="Login1" runat="server" CssClass="LoginControl">  
  10.             <TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="#FFFFFF" />  
  11.         </asp:Login>  
  12.     </div>  
  13.     </form>  
  14. </body>  
  15. </html>  
If you running the page and if the CSS file is placed in a directory where anonymous access is denied, the add the following configuration for the CSS file to you web.config file.
  1. <location path="StyleSheet.css">  
  2. <system.web>  
  3. <authorization>  
  4. <allow users="*"/>  
  5. </authorization>  
  6. </system.web>  
  7. </location>  
You can add several hyperlinks to your Login control, such as hyperlink to a help text page, or a hyperlink to to a registration page.
  1. <asp:Login ID="Login1" runat="server" CssClass="LoginControl"  
  2. CreateUserText="Register"  
  3. CreateUserUrl="~/Register.aspx"  
  4. HelpPageText="Additional Help" HelpPageUrl="~/Help.aspx"  
  5. InstructionText="Please enter your user name and password for login.">  
  6. <TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="#FFFFFF" />  
  7. </asp:Login>  
Looks like this :

Here is .CS Code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data.SqlClient;  
  8. public partial class _Default : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.         if (!this.IsPostBack)  
  13.             ViewState["LoginErrors"] = 0;  
  14.     }  
  15.    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)  
  16.     {  
  17.         if (YourValidationFunction(Login1.UserName, Login1.Password))  
  18.         {  
  19.            // e.Authenticated = true;  
  20.             Login1.Visible = false;  
  21.             MessageLabel.Text = "Successfully Logged In";  
  22.         }  
  23.         else  
  24.         {  
  25.             e.Authenticated = false;  
  26.         }  
  27.     }  
  28.     protected void Login1_LoginError(object sender, EventArgs e)  
  29.     {  
  30.         if (ViewState["LoginErrors"] == null)  
  31.             ViewState["LoginErrors"] = 0;  
  32.         int ErrorCount = (int)ViewState["LoginErrors"] + 1;  
  33.         ViewState["LoginErrors"] = ErrorCount;  
  34.         if ((ErrorCount > 3) && (Login1.PasswordRecoveryUrl != string.Empty))  
  35.             Response.Redirect(Login1.PasswordRecoveryUrl);  
  36.     }  
  37.     private bool YourValidationFunction(string UserName, string Password)  
  38.     {  
  39.         bool boolReturnValue = false;         
  40.         string strConnection = "server=.;database=Vendor;uid=sa;pwd=wintellect;";  
  41.         SqlConnection sqlConnection = new SqlConnection(strConnection);  
  42.         String SQLQuery = "SELECT UserName, Password FROM Login";  
  43.         SqlCommand command = new SqlCommand(SQLQuery, sqlConnection);  
  44.         SqlDataReader Dr;  
  45.         sqlConnection.Open();  
  46.         Dr = command.ExecuteReader();  
  47.         while (Dr.Read())  
  48.         {  
  49.             if ((UserName == Dr["UserName"].ToString()) & (Password == Dr["Password"].ToString()))  
  50.             {  
  51.                 boolReturnValue = true;  
  52.             }  
  53.             Dr.Close();  
  54.             return boolReturnValue;  
  55.         }  
  56.         return boolReturnValue;  
  57.     }  
  58. }  
If you insert wrong username and password then message will show like this: 

If you insert right usename, password then redirect your page whereever you want or you can show message in ErrorLabel, Like this:

I am attaching my database with application in App_Data folder, if u want use my database then attach my .MDF file.

Any question and queries ask me any time.

Similar Articles