Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Session ID remains the same before login and after login
How to fix it.

when i login with login page then i want to change the session id.
means when i open login page then i have a one ASP.NET_SessionId after login this ASP.NET_SessionId remain same.
so i want to change this ASP.NET_SessionId after login and remain task will going on with new ASP.NET_SessionId . so can you tell me how to do.

I am very thankful to you if you reply for that.
Posted
Comments
What do you exactly mean by changing the session? Do you need to assign some new value to it?
Please explain.

Please change session id while you are loggin into the account like

VB
Session["userid"] = userId


when you are loggin out use method

VB
Session.abandon()
 
Share this answer
 
Just Invoke this Function, Wherever you need to change the ASP.NET_Session ID

VB
Sub RegenerateID()
        Dim manager
        Dim oldId As String
        Dim newId As String
        Dim isRedir As Boolean
        Dim isAdd As Boolean
        Dim ctx As HttpApplication
        Dim mods As HttpModuleCollection
        Dim ssm As System.Web.SessionState.SessionStateModule
        Dim fields() As System.Reflection.FieldInfo
        Dim rqIdField As System.Reflection.FieldInfo
        Dim rqLockIdField As System.Reflection.FieldInfo
        Dim rqStateNotFoundField As System.Reflection.FieldInfo

        Dim _nbrqPath As System.Reflection.FieldInfo


        Dim store As SessionStateStoreProviderBase
        Dim field As System.Reflection.FieldInfo
        Dim lockId
        manager = New System.Web.SessionState.SessionIDManager
        oldId = manager.GetSessionID(Context)
        newId = manager.CreateSessionID(Context)
        manager.SaveSessionID(Context, newId, isRedir, isAdd)
        ctx = HttpContext.Current.ApplicationInstance
        mods = ctx.Modules
        ssm = CType(mods.Get("Session"), System.Web.SessionState.SessionStateModule)
        fields = ssm.GetType.GetFields(System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance)
        store = Nothing : rqLockIdField = Nothing : rqIdField = Nothing : rqStateNotFoundField = Nothing : _nbrqPath = Nothing
        For Each field In fields
            If (field.Name.Equals("_store")) Then store = CType(field.GetValue(ssm), SessionStateStoreProviderBase)
            If (field.Name.Equals("_rqId")) Then rqIdField = field
            If (field.Name.Equals("_rqLockId")) Then rqLockIdField = field
            If (field.Name.Equals("_rqSessionStateNotFound")) Then rqStateNotFoundField = field

        Next
        lockId = rqLockIdField.GetValue(ssm)
        If ((Not IsNothing(lockId)) And (Not IsNothing(oldId))) Then store.ReleaseItemExclusive(Context, oldId, lockId)
        rqStateNotFoundField.SetValue(ssm, True)
        rqIdField.SetValue(ssm, newId)
    End Sub
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900