Home
%3CLINGO-SUB%20id%3D%22lingo-sub-714318%22%20slang%3D%22en-US%22%3EThe%20issue%20of%20generating%20a%20new%20session%20ID%20after%20postbacks%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-714318%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20%3CSTRONG%3Esession%20ID%3C%2FSTRONG%3E%20should%20remain%20the%20same%20for%20the%20same%20visitor%20in%20the%20same%20connection.%20If%20the%20session%20ID%20changes%20between%20pages%2C%20It%20will%20likely%20to%20break%20your%20application%20especially%20if%20you%20are%20using%20session%20ID%20to%20improve%20your%20%3CSTRONG%3EViewState%3C%2FSTRONG%3E%20complexity.%20An%20example%20to%20this%20usage%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CCODE%3EPage.ViewStateUserKey%20%3D%20Session.SessionID%3B%3C%2FCODE%3E%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-1592259031%22%20id%3D%22toc-hId-1592259031%22%3E%26nbsp%3B%3C%2FH3%3E%0A%3CH3%20id%3D%22toc-hId--959897930%22%20id%3D%22toc-hId--959897930%22%3EHow%20to%20keep%20the%20session%20ID%20same%3C%2FH3%3E%0A%3COL%3E%0A%3CLI%3EMake%20sure%20not%20to%20create%20session%20cookie%20repeatedly.%20If%20the%20code%20that%20creates%20session%20cookie%20is%20executed%20between%20postbacks%2C%20you%20will%20end%20up%20having%20a%20different%20session%20ID%20because%20the%20session%20cookie%20is%20recreated.%3CBR%20%2F%3E%3CCODE%3EResponse.Cookies.Add(new%20HttpCookie(%22id%22%2C%20%22%22))%3C%2FCODE%3E%3C%2FLI%3E%0A%3CLI%3EMake%20sure%20no%20to%20generate%20session%20ID%20repeatedly.%20An%20example%20code%20of%20session%20ID%20creation%3A%20%3CBR%20%2F%3E%3CCODE%3EobjManageSession.CreateSessionID(this.Context)%3B%3C%2FCODE%3E%3CCODE%3E%3C%2FCODE%3E%3C%2FLI%3E%0A%3CLI%3EAssign%20a%20dummy%20value%20to%20your%20session%20cookie%20in%20%3CCODE%3EGlobal.asax%3C%2FCODE%3E%3A%3CBR%20%2F%3E%3CCODE%3Eprotected%20void%20Session_Start(object%20sender%2C%20EventArgs%20e)%3CBR%20%2F%3E%7B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3CBR%20%2F%3E%2F%2F%20It%20adds%20an%20entry%20to%20the%20Session%20object%20so%20the%20sessionID%20is%20kept%20for%20the%20entire%20session%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3CBR%20%2F%3ESession%5B%22init%22%5D%20%3D%20%22session%20start%22%3B%3CBR%20%2F%3E%7D%3C%2FCODE%3E%3CCODE%3E%3C%2FCODE%3E%3C%2FLI%3E%0A%3CLI%3EAssign%20a%20dummy%20value%20to%20your%20session%20cookie%20in%20%3CCODE%3EPage_Load%3C%2FCODE%3E%20method%20of%20your%20homepage%3A%3CBR%20%2F%3E%3CCODE%3Eprotected%20void%20Page_Load(object%20sender%2C%20EventArgs%20e)%7B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Session%5B%22init%22%5D%20%3D%20%22session%20start%22%3B%7D%3C%2FCODE%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CH4%20id%3D%22toc-hId-586398900%22%20id%3D%22toc-hId-586398900%22%3E%26nbsp%3B%3C%2FH4%3E%0A%3CH4%20id%3D%22toc-hId--1965758061%22%20id%3D%22toc-hId--1965758061%22%3E%3CSTRONG%3EIt%20works%20in%20the%20server%20but%20not%20in%20localhost%3F%3C%2FSTRONG%3E%3C%2FH4%3E%0A%3CP%3EIn%20your%20web.config%20file%2C%20change%20the%20value%20of%20%3CCODE%3EhttpOnlyCookies%3C%2FCODE%3E%20and%20%3CCODE%3ErequireSSL%3C%2FCODE%3E%20parameters%20to%20%3CCODE%3Efalse%3C%2FCODE%3E.%20If%20they%20are%20set%20to%20%3CCODE%3Etrue%3C%2FCODE%3E%2C%20the%20local%20server%20will%20force%20the%20application%20to%20regenerate%20session%20between%20pages.%20Make%20sure%20to%20switch%20these%20values%20back%20to%20true%20before%20you%20migrate%20your%20code%20to%20the%20server.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CCODE%3E%3CHTTPCOOKIES%20httponlycookies%3D%22false%22%20requiressl%3D%22false%22%3E%3C%2FHTTPCOOKIES%3E%3C%2FCODE%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-714318%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20style%3D%22display%3A%20inline%20!important%3B%20float%3A%20none%3B%20background-color%3A%20%23ffffff%3B%20color%3A%20%23333333%3B%20cursor%3A%20text%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3EThe%20%3C%2FSPAN%3E%3CSTRONG%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20bold%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3Esession%20ID%3C%2FSTRONG%3E%3CSPAN%20style%3D%22display%3A%20inline%20!important%3B%20float%3A%20none%3B%20background-color%3A%20%23ffffff%3B%20color%3A%20%23333333%3B%20cursor%3A%20text%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%20should%20remain%20the%20same%20for%20the%20same%20visitor%20in%20the%20same%20connection.%20If%20the%20session%20ID%20changes%20between%20pages%2C%20It%20will%20likely%20to%20break%20your%20application%20especially%20if%20you%20are%20using%20session%20ID%20to%20improve%20your%20%3C%2FSPAN%3E%3CSTRONG%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20bold%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3EViewState%3C%2FSTRONG%3E%3CSPAN%20style%3D%22display%3A%20inline%20!important%3B%20float%3A%20none%3B%20background-color%3A%20%23ffffff%3B%20color%3A%20%23333333%3B%20cursor%3A%20text%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%20complexity.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-714318%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Epostback%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESession%20ID%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EViewState%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

The session ID should remain the same for the same visitor in the same connection. If the session ID changes between pages, It will likely to break your application especially if you are using session ID to improve your ViewState complexity. An example to this usage:

 

Page.ViewStateUserKey = Session.SessionID;

 

How to keep the session ID same

  1. Make sure not to create session cookie repeatedly. If the code that creates session cookie is executed between postbacks, you will end up having a different session ID because the session cookie is recreated.
    Response.Cookies.Add(new HttpCookie("id", ""))
  2. Make sure no to generate session ID repeatedly. An example code of session ID creation:
    objManageSession.CreateSessionID(this.Context);
  3. Assign a dummy value to your session cookie in Global.asax:
    protected void Session_Start(object sender, EventArgs e)
    {    
    // It adds an entry to the Session object so the sessionID is kept for the entire session   
    Session["init"] = "session start";
    }
  4. Assign a dummy value to your session cookie in Page_Load method of your homepage:
    protected void Page_Load(object sender, EventArgs e){     Session["init"] = "session start";}

 

It works in the server but not in localhost?

In your web.config file, change the value of httpOnlyCookies and requireSSL parameters to false. If they are set to true, the local server will force the application to regenerate session between pages. Make sure to switch these values back to true before you migrate your code to the server.

 

<httpCookies httpOnlyCookies="false" requireSSL="false"/>