locked
why my session lost ? RRS feed

  • Question

  • User-535616387 posted

    Dear Guys,

    i have 2 page :

    - "login.aspx"
    - "default.aspx"    

    I add session in login.aspx but why my session lost in default.aspx page ?

    below is my code :

    Inside login.aspx 

    Session.Add("LoginName", "Michael")

    Inside default.aspx

    If Session("LoginName") > 0 Then
       Dim user as string 
       user =  Session("LoginName")
    Else
       Response.Redirect("Logout.aspx")
    End If

    inside default.aspx session LoginName always return null.

    please advice why ?

    Thank & Regards,
    Wibowo Wiwit, S.kom

    Friday, December 28, 2018 7:25 AM

All replies

  • User-1716253493 posted

    try this

    If Session("LoginName") isnot nothing Then

    Friday, December 28, 2018 9:07 AM
  • User-271186128 posted

    Hi wibowowiwit,

    wibowowiwit

    inside default.aspx session LoginName always return null.

    please advice why ?

    First, try to set break point in the login page, and make sure you have set the value in the session. 

    Then, try to use the following code to check whether the session is exist or not:

            If Session("LoginName") IsNot Nothing Then
                Dim user As String
                user = Session("LoginName")
            Else
                Response.Redirect("Logout.aspx")
            End If

    If still can't get the login name, please check your code or in the web.config file, whether you have set the session timeout property.

    <configuration>  
      <system.web>  
        <sessionState   
          mode="InProc"  
          cookieless="true"  
          timeout="30" />  
      </system.web>  
    </configuration>  

    Also, you could try to change the SessionStateMode to SQLServer or StateServer. 

    More details about session, please check this article.

    Best regards,
    Dillion

    Saturday, December 29, 2018 3:11 AM
  • User-535616387 posted

    Hi Zhi Lv,

    thankyou for answering my thread,

    this occurs after i convert my project from asp.net 2 to asp.net 4

    i've set the value in session. 

    the session value only available at the page i set the session, after i change another aspx page the session return null.

    i've try using below code and it works, but my url changes from http://localhost:90/default.aspx  to http://localhost:90/default.aspx/(S(mzlh1kmzg3tukm5v5tfrrhcw))  : 

    please suggest how to remove the "(S(mzlh1kmzg3tukm5v5tfrrhcw))" in url ?

    <configuration>  
      <system.web>  
        <sessionState   
          mode="InProc"  
          cookieless="true"  
          timeout="30" />  
      </system.web>  
    </configuration> 

    Best Regards,
    Wibowo Wiwit.

    Thursday, January 3, 2019 3:38 AM
  • User738333362 posted

    Try this

    If (Session IsNot Nothing)
        Dim item as Object = Session("LoginName")
        If (item IsNot Nothing)
        ...
        End If
    End If
    Thursday, January 3, 2019 7:20 AM
  • User-535616387 posted

    Dear Oned Gk & Shivanand,

    thankyou for answering my thread,

    The session still return Null in next page.

    the session only available in the first page after i assign value to the session.

    Best Regards,
    Wibowo Wiwid.

    Thursday, January 3, 2019 7:59 AM
  • User-271186128 posted

    Hi wibowowiwit

    How do you redirect to the default page? If you are using master pages check if session.abandon() is not called, put a break point on response.redirect and go through step by step and check on which step it goes null.

    If still not working, you could try to create a new project to test part of code, or you can share your whole code on onedrive, so that we could test it on our side.

    Best regards,
    Dillion

    Thursday, January 3, 2019 9:33 AM
  • User753101303 posted

    Hi,

    You are redirecting once the Session variable is set? I think I should test whenever seeing that but it seems some (old) browser version are not handling the same way a response that comes with both new cookies and a redirect header. Which browser do you use ?

    If this is  just to store the current user name it could be best to just use what ASP.NET offers out of the box for handling authentication (System.Web.HttpContext.Current.User which is exposed as many places in Web Forms and MVC).

    Thursday, January 3, 2019 2:34 PM