RidwanA.com

asp.net simple session making PDF Print E-mail
  
Friday, 28 March 2008 17:53
Today i learned about session making using asp.net 2008 with vb
in this example, you need to create a login page and a valid session only page.
steps:
1) In login.aspx page, there is one button and two textbox for user and password (later for database user checking)

2) now you create a session.
syntax: session(key)=value
code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Session(TextBox1.Text) = TextBox2.Text
        Dim sessioncookie As HttpCookie = New HttpCookie("usersession")
        sessioncookie.Value = Session.SessionID
        Response.Cookies.Add(sessioncookie)
        Response.Redirect("default.aspx")
End Sub

explanation:
you put user name in textbox1 as session key
you put user password in textbox2 as session value
you create cookie named usersession to store current SessionID
you then move to another page (default.aspx)

3) In default.vb you create sub page load. So every time the page refresh, it will trigger the sub.

code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session.SessionID <> Request.Cookies("usersession").Value or Request.Cookies("usersession") Is Nothing Then
            Response.Redirect("login.aspx")
        End If
End Sub


explanation:
you create a cookie checking value if the usersession cookie still hold the same value as the current SessionID.
if false then you automatically redirect the page to login.aspx

4) extra note: this session method only tested in this kind environment:
 session name will have different sessionID every time a session expired.
<sessionState cookieless="UseUri" timeout="1" regenerateExpiredSessionId="false" />
Last Updated ( Saturday, 29 March 2008 14:06 )
 

Add your comment

Your name:
Your email:
Subject:
Comment:
  The word for verification. Lowercase letters only with no spaces.
Word verification:

Polls

where are you come from?
 

Events Calendar

January 2009
M T W T F S S
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1

Advertisement

Featured Links:
Joomla!
Joomla! The most popular and widely used Open Source CMS Project in the world.
JoomlaCode
JoomlaCode, development and distribution made easy.
Joomla! Extensions
Joomla! Components, Modules, Plugins and Languages by the bucket load.
Joomla! Shop
For all your Joomla! merchandise.
Banner
Copyright © 2009 RidwanA.com. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.

Google Analytics Tracking Module

Proudly Hosted By
web hosting indonesia