Click here to Skip to main content
11,929,944 members (54,030 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked

Backup and Restore Session in .Net 2.0

, 25 Jan 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
When you switch user or emulate the user this is very useful


This code backup your session in byte stream. It is useful when you swith the user or emulate the user and need to clear the current session. you can store this byte stream in newly created session as a session veriable and it is so simple to retrive the old session data from that byte stream. This is very useful when you are using SQL memebership provider and maintaining signin and signout using cookies.


I hope you have some idea how session works so I am not going to put the code here to show you how to handle session.

Using the code

This code got 2 methods onr is to backup the session and one is to restore the session. I think ou can use this two methods without any change in the code because it is very generic.

Blocks of code should be set as style "Formatted" like this:

Function BackupSession() As Byte()

    Dim objSessionData As New SessionState.SessionStateItemCollection

    Dim objStream As New System.IO.MemoryStream

    Dim objWriter As New System.IO.BinaryWriter(objStream)

    For i As Integer = 0 To Me.Session.Count - 1

        objSessionData.Item(Me.Session.Keys(i)) = Me.Session.Item(i)



    Return objStream.ToArray

End Function

Sub RestoreSession(ByVal prevSessionData As Byte())

    Dim objStream As New System.IO.MemoryStream

    Dim objRedaer As New System.IO.BinaryReader(objStream)

    Dim objPrevSession As SessionState.SessionStateItemCollection

    objStream.Write(prevSessionData, 0, prevSessionData.Length)

    objStream.Seek(0, IO.SeekOrigin.Begin)

    objPrevSession = SessionState.SessionStateItemCollection.Deserialize(objRedaer)

    For Each key As String In objPrevSession.Keys

        Me.Session(key) = objPrevSession.Item(key).ToString


End Sub

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim prevSessionData As Byte()

    prevSessionData = Me.BackupSession()


    'Signout and clear all the cookies of the user.
    'Signin new user using new cookie and create new session.
    Session("PrevSession") = prevSessionData

    'Now when you swith user or cancel emulation, I mean go back to original user.
    prevSessionData = nothing

    prevSessionData = CType(Session("PrevSession"), Byte())
    'Do signin and signout here then

End Sub

Points of Interest

I spend more than 2 hours to find code to do this and then also I didn't find the exact same code that's why I just want to share this code with you, incase anybody need to do this kind of stuff. I need to do this because administrator need to emulate user all the time and when he cancel the emulation he lost all his information that's how I come with this solution.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Questionwhat namespace? Pin
BhaveshRadhik4-Feb-08 5:15
memberBhaveshRadhik4-Feb-08 5:15 
AnswerRe: what namespace? Pin
Crooze214-Feb-08 5:33
memberCrooze214-Feb-08 5:33 
Questionbut why do it? Pin
Thanks for all the fish25-Jan-08 11:00
memberThanks for all the fish25-Jan-08 11:00 
AnswerRe: but why do it? Pin
Crooze2125-Jan-08 11:05
memberCrooze2125-Jan-08 11:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 25 Jan 2008
Article Copyright 2008 by Crooze21
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid