ASP.Net Login Page Using VB.Net
This sample illustrate how to create a login page in an ASP.NET application using vb.NET, it uses Access database to authenticate users.
First let's create an Access database login.mdb. Then create a table "Users" with two columns : Username and Password.
The connection string will be placed in the webconfig file as the following:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="strConn" value="Provider = Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\login.mdb;User ID=Admin;Password=;" />
</appSettings>
<system.web>
....
...
Set the authentication as the following:
<authentication mode="Forms">
<forms name="frmLogin" loginUrl="Login.aspx" />
</authentication>
Now, create a new web form, and save it as frmLogin
Paste the following got into the new form:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Login.aspx.vb" Inherits="loginSample.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> Login Form </title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<style type="text/css">
<!--
.style1 {color: #FF0000}
-->
</style>
</head>
<body>
<form id="frmlogin" method="post" runat="server">
<asp:label id="lblMessage" runat="server" width="288px" font-bold="True" font-italic="True"
font-size="Medium" forecolor="#C00000"></asp:label>
<table id="mainTable" style="POSITION: absolute; TOP: 145px; left: 208px;">
<tr>
<td>
<table cellspacing="3" bordercolor="#0000FF" bgcolor="#999999" id="loginTbl" style="BORDER-RIGHT: #6699cc solid; BORDER-TOP: #6699cc solid; FONT-WEIGHT: bold; LEFT: 30%; BORDER-LEFT: #6699cc solid; BORDER-BOTTOM: #6699cc solid; FONT-FAMILY: Sans-Serif; BACKGROUND-COLOR: lightgrey">
<tr>
<td bgcolor="#CCCCCC"><b>Username: </b> </td>
<td bgcolor="#CCCCCC">
<asp:textbox id="txtUserName" runat="server" width="160px"></asp:textbox>
<span class="style1">
<asp:requiredfieldvalidator runat="server" id="rvUserValidator" controltovalidate="txtUserName" errormessage="Username Required!"
display="None" />
</span> </td>
</tr>
<tr>
<td bgcolor="#CCCCCC"><b>Password: </b> </td>
<td bgcolor="#CCCCCC">
<asp:textbox id="txtPassword" runat="server" textmode="Password" width="160px"></asp:textbox>
<span class="style1">
<asp:requiredfieldvalidator runat="server" id="rvPasswordValidator" controltovalidate="txtPassword" errormessage="Password cant be blank!"
display="None" />
</span> </td>
</tr>
<tr>
<td colspan="2" align="center" bgcolor="#CCCCCC"><asp:button id="btnLogin" text="Login" runat="server" borderstyle="Solid"></asp:button></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table id="messageDisplay">
<tr>
<td><asp:validationsummary runat="server" displaymode="BulletList" id="Validationsummary1" width="472px" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
Go to design view mode, take a look at the design.
Import the following name spaces
Imports System.Web.Security
Imports System.Data
Imports System.Data.OleDb
Imports System.Configuration
Now, add this code to button click event
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
If Page.IsValid Then
If validate_user(txtUserName.Text, txtPassword.Text) Then
FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, False) ' Redirect to default page
Else
lblMessage.Text = "Invalid login!"
End If
End If
End Sub
Now write a the validate function
Function validate_user(ByVal tUser As String, ByVal tPassword As String) As Boolean
Dim sqlLogin As String
Dim cmd As New OleDbCommand(sqlLogin, conn)
cmd.CommandType = CommandType.Text
Dim conn As OleDbConnection = New OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
sqlLogin=SELECT Count(*) from Users where username='"& tUser &"' and password='" & tpassword&"'"
cmd.commandtext = sqlLogin
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
Dim objReader As OleDbDataReader
objReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While objReader.Read()
If CStr(objReader.GetValue(0)) <> "1" Then
Return False
Else
objReader.Close()
Return True
End If
End While
End Function
Go to the database file, Insert new records in it. Then compile your web application, and try to login. That's it!
Happy coding :)
- ash87's blog
- Login or register to post comments
- 129 reads
good job
Nice code ash78 ;)
Keep it up!