ARTICLE

FormsAuthentication

Posted by Gurjeet Singh Articles | ASP.NET using VB.NET June 24, 2011
FormsAuthentication class you can build a registration form of user and also authenticate system.
Download Files:
 
Reader Level:

The .NET classes for Forms authentication are located in the System.Web.Security namespace. Forms authentication helps to build automatic user registration system. You should use FormsAuthentication class that contains several methods for working with Forms authentication. You can easily store username and password in your desire storage mechanism this is the main advantage of authentication. For example, you can store usernames and passwords in the Web.Config file located in an application's root directory.
 
You must enable Forms authentication for an ASP.NET application as a whole. Forms authentication trusts on browser cookies to find out the individuality of a user. Using FormsAuthentication class you can build a registration form of user and also authenticate system without using ASP.NET Membership. Authentication is accomplished using credentials. The credentials are then validated against some authority.

Login.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Login.aspx.vb" Inherits="Login" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
    <title></title>
</
head>
<
body>
    <form id="form1" runat="server">

    <div>
    <asp:Label ID="idError" EnableViewState="false" ForeColor="Red" runat="server" />
        <br />
        <br />
        <asp:Label ID="Name" Text="User Name:" AssociatedControlID="idtxtName" runat="server" />
        <br />
        <asp:TextBox ID="idtxtName" runat="server" />
        <br />
        <br />
        <asp:Label ID="Password" Text="Password:" AssociatedControlID="idtxtPswd" runat="server" />
        <br />
        <asp:TextBox ID="idtxtPswd" TextMode="Password" runat="server" />
        <br />
        <br />
        <asp:CheckBox ID="idchkRememberMe" Text="Remember Me" runat="server" />
        <br />
        <br />
        <asp:Button ID="idbtnlogin" Text="Login" OnClick="idbtnlogin_Click" runat="server" />
    </div>
    </form>

</
body>
</
html>

Login.aspx.vb


Partial
Class Login
    Inherits System.Web.UI.Page
    Protected Sub idbtnlogin_Click(sender As Object, e As System.EventArgs) Handles idbtnlogin.Click
        If FormsAuthentication.Authenticate(idtxtName.Text, idtxtPswd.Text) Then
            FormsAuthentication.RedirectFromLoginPage(idtxtName.Text, idchkRememberMe.Checked)
            Response.Redirect("~/welcome.aspx")
        Else
            idError.Text = "Invalid user name/password"
        End If
    End Sub

End
Class

Web.config


<?
xml version="1.0"?>
<
configuration>
  <
system.web>
    <
authentication mode="Forms">
      <
forms>
        <
credentials passwordFormat="Clear">
          <
user name="Clark" password="password123"/>
          <
user name="Ricky" password="password@123"/>
          <
user name="Hussey" password="password@1"/>
        </
credentials>
      </
forms>
    </
authentication>
    <
compilation debug="true" strict="false" explicit="true" targetFramework="4.0"/>
  </
system.web>
</
configuration>

Welocme.aspx


<%
@ Page Language="VB" AutoEventWireup="false" CodeFile="welcome.aspx.vb" Inherits="welcome" %>
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
   <title></title>

</
head>
<
body>
    <form id="form1" runat="server">
    <div>
    <h2 style="color:Red;font-weight:bold">Hi</h2>
    <br />
     <h2 style="color:Red;font-weight:bold">Welcome to our website.</h2>
    </div>
    </form>

</
body>
</
html>

Output

Untitled-1.gif

Figure1

Click on Login button

Output

2.gif

Figure2

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • 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.
    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.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor