Creating a Simple Secured Portal through Session Variables in ASP.NET
Posted on January 16, 2008 - Filed Under ASP.NET |
This is a simple tutorial on how to create a simple secured website with the use of Session variables in ASP.Net. It will also demonstrate some of the basics of an ASP.Net page
With Visual Web Developer Express, create a new Website. Click on the Website Menu from the Menu Bar and add two more Webforms (ASP.Net pages) that do not use the code behind technique. Name the first one as login.aspx and the other one as mainpage.aspx. You can then remove the default.aspx and its vb code file from the solution explorer. With this, we simply mean, we are not going to use the code behind method in our application.
In the login.aspx page, type the following codes:
——————————————–
<%@ Page Language=”VB” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<script runat=”server”>
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
If Me.TextBox1.Text = “administrator” And Me.TextBox2.Text = “123″ Then
Session(”varusername”) = Me.TextBox1.Text.Trim
Response.Redirect(”mainpage.aspx”)
Else
Me.Label3.Text = “Message : Invalid Username or Password! Access Denied.”
Session(”varusername”) = vbNullString
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Not IsPostBack Then
Session(”varusername”) = vbNullString
Me.Label3.Text = “Message :”
�
End If
End Sub
</script>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>My Portal</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<h1>LOGIN PAGE</h1>
<table align=”center”>
<tr>
<td>
<asp:Label ID=”Label1″ runat=”server” Text=”Username”></asp:Label></td>
<td>
<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox></td>
</tr>
<tr>
<td>
<asp:Label ID=”Label2″ runat=”server” Text=”Password”></asp:Label></td>
<td>
<asp:TextBox ID=”TextBox2″ runat=”server” TextMode=”Password”></asp:TextBox></td>
</tr>
<tr>
<td colspan=”2″ align=”center”>
<asp:Button ID=”Button1″ runat=”server” Text=”Login” OnClick=”Button1_Click” /></td>
�
</tr>
</table>
<div align=”center”>
<asp:Label ID=”Label3″ runat=”server” Text=”Message:”></asp:Label>
</div>
</div> �
</form>
</body>
</html>
——————————————–
Place these codes in the mainpage.aspx page:
——————————————–
<%@ Page Language=”VB” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<script runat=”server”>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Session(”varusername”) = vbNullString Then
Response.Redirect(”login.aspx”)
End If
Response.Write(Session.SessionID)
Me.Label1.Text = “Welcome! ” & Session(”varusername”)
�
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Session.Clear()
Response.Redirect(”login.aspx”)
�
End Sub
</script>
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>My Portal Mainpage</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:Label ID=”Label1″ runat=”server” Font-Size=”XX-Large” Width=”612px”></asp:Label>
<hr />
<asp:Button ID=”Button1″ runat=”server” Text=”Logout” OnClick=”Button1_Click” />
</div>
</form>
</body>
</html>
——————————————–
Finally, make sure you have the web.config file in your website folder. This file is automatically created in Visual Web Developer and it is very essential in order for your application to work properly so do not remove this file.
I strongly advise that you type the given codes yourself instead of just copying and pasting them because some of the characters are being changed by wordpress’ css and might cause you some errors in your application. This is a well tested application and is 100% guaranteed working. If ever you will not be able to make it work please check carefully how you typed the codes.
Comments
Leave a Reply