Blue Theme Orange Theme Green Theme Red Theme
 
ASP.NET Web Hosting – Click Here
Home | Forums | Videos | Photos | Blogs | Beginners
 | Consulting  
Submit an Article 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » ASP.NET and Web » Login Control in ASP.NET 3.5 using VB.NET

Login Control in ASP.NET 3.5 using VB.NET

In this article, I am going to discuss how to use Login control in ASP.NET 3.5 using VB.NET step by step.

Author Rank:
Technologies: .NET 2.0, ASP.NET 3.5,Visual Basic .NET
Total downloads : 512
Total page views :  24461
Rating :
 5/5
This article has been rated :  2 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
VB.NETLoginControl.zip
 
ArticleAd
Become a Sponsor


Related EbooksTop Videos

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.

  • Start Microsoft Visual Studio 2008
  • Create a new ASP.NET WebSite using Visual Basic, Like this:



  • 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.

<form id="form1" runat="server">

<div>

<asp:Login ID="Login1" runat="server" BackColor="#F7F7DE" BorderColor="#CCCC99" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="10pt">

<TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="#FFFFFF" />

</asp:Login>

    </div>

    </form>

You can change styles of LoginControl using css too,  Like this:

.LoginControl

{

          background-color:#F7F7DE;

          border-color:#CCCC99;

          border-style:solid;

    border-width:1px;

    font-family:Verdana;

    font-size:10px;    

}

And now apply css to control:

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Login Control</title>

    <link href="StyleSheet.css" type="text/css" rel="Stylesheet" />

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:Login ID="Login1" runat="server" CssClass="LoginControl">

            <TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="#FFFFFF" />

        </asp:Login>

    </div>

    </form>

</body>

</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. 

<location path="StyleSheet.css">

<system.web>

<authorization>

<allow users="*"/>

</authorization>

</system.web>

</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.

<asp:Login ID="Login1" runat="server" CssClass="LoginControl"

CreateUserText="Register"

CreateUserUrl="~/Register.aspx"

HelpPageText="Additional Help" HelpPageUrl="~/Help.aspx"

InstructionText="Please enter your user name and password for login.">

<TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="#FFFFFF" />

</asp:Login>

Looks like this :

Here is .VB Code:

Imports System

Imports System.Collections.Generic

Imports System.Linq

Imports System.Web

Imports System.Web.UI

Imports System.Web.UI.WebControls

Imports System.Data.SqlClient

 

Partial Class _Default

    Inherits System.Web.UI.Page

 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Me.IsPostBack Then

            ViewState("LoginErrors") = 0

        End If

    End Sub

 

    Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As AuthenticateEventArgs)

        If YourValidationFunction(Login1.UserName, Login1.Password) Then

            ' e.Authenticated = true;

            Login1.Visible = False

            MessageLabel.Text = "Successfully Logged In"

        Else

            e.Authenticated = False

        End If

    End Sub

 

    Protected Sub Login1_LoginError(ByVal sender As Object, ByVal e As EventArgs)

        If ViewState("LoginErrors") Is Nothing Then

            ViewState("LoginErrors") = 0

        End If

 

        Dim ErrorCount As Integer = CInt(ViewState("LoginErrors")) + 1

        ViewState("LoginErrors") = ErrorCount

 

        If (ErrorCount > 3) AndAlso (Login1.PasswordRecoveryUrl <> String.Empty) Then

            Response.Redirect(Login1.PasswordRecoveryUrl)

        End If

    End Sub

 

    Private Function YourValidationFunction(ByVal UserName As String, ByVal Password As String) As Boolean

        Dim boolReturnValue As Boolean = False

        Dim strConnection As String = "server=.;database=Vendor;uid=sa;pwd=wintellect;"

        Dim sqlConnection As New SqlConnection(strConnection)

        Dim SQLQuery As String = "SELECT UserName, Password FROM Login"

        Dim command As New SqlCommand(SQLQuery, sqlConnection)

        Dim Dr As SqlDataReader

        sqlConnection.Open()

        Dr = command.ExecuteReader()

        While Dr.Read()

            If (UserName = Dr("UserName").ToString()) And (Password = Dr("Password").ToString()) Then

                boolReturnValue = True

            End If

            Dr.Close()

            Return boolReturnValue

        End While

        Return boolReturnValue

    End Function

End Class

If you insert wrong username and password then message will show like this:

If you insert correct 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 to use my database then attach my .MDF file.

Any question and queries ask me any time.


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Rajkumar Beniwal
Raj working as a senior software engineer has 4 years experience working on ASP.NET 2.0, VB.NET,C#, AJAX,SQL and other latest technologies. He holds Master's degree in Computer Science. currently using WPF, WCP, silverlight.
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.
Boost the performance of your .NET applications
“ANTS Profiler took us straight to the specific areas of our code which were the cause of our performance issues." Terry Phillips, Sr. Developer, Harley-Davidson Dealer Systems. Download your free trial of ANTS Profiler.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
VB.NETLoginControl.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
ArticleAd
Become a Sponsor
Latest Comments:
Subject Posted By Posted On
Errorfateemah10/23/2008
Hello, I have been trying to implement the above code but have been met with quite a few problems Firstly, the Imports are not accepted and secondly, i keep on getting the error message " reference to a non shared reference requires an object reference" for "Login1" which is underlined in blue in this part of the code:- Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As AuthenticateEventArgs) If YourValidationFunction(Login1.UserName, Login1.Password) Then ' e.Authenticated = true; Login1.Visible = False MessageLabel.Text = "Successfully Logged In" Else e.Authenticated = False End If End Sub Please help me if possible...I am pretty stuck with this problem and really need help thanks
Reply | Email | Delete | Modify | 
solutionUrielz11/24/2008
Hello, fateemah: This error happens because surely you have a LOGIN control within a LOGINVIEW control. I have had the same error. I solved this by seeking LOGIN control within the LOGINVIEW control. Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As AuthenticateEventArgs) Dim objLogin As System.Web.UI.WebControls.Login = CType(LoginView1.FindControl("Login1"), System.Web.UI.WebControls.Login) If YourValidationFunction(objLogin.UserName, objLogin.Password) Then e.Authenticated = True objLogin.Visible = False MessageLabel.Text = "Successfully Logged In" Else e.Authenticated = False End If End Sub I hope you will help
Reply | Email | Delete | Modify | 
Smart tag content!Naive5/12/2009
Hi there. I have a question. Previously, in Visual Studio 2005, login control had template editing; such as "logged in" template, "anonymous" template, etc. These were available through the smart tag. What happened to those?!
Reply | Email | Delete | Modify | 
 
 
Re: Smart tag content!Rajkumar5/16/2009
may be something wrong with your visual studio. i can see in my visual studio and that is LoginStatus control not Login control.
Reply | Email | Delete | Modify | 
feedbackashish6/30/2009
thanks for your programming concept


   
Reply | Email | Delete | Modify | 

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved