ASP.NET FormsAuthentication using VB.NET

FormsAuthentication class you can build a registration form of user and also authenticate system.
  • 2523
 

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

untiled2.gif

Figure2

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.