Having Trouble with Cookies - Migrating from Web1.0 to 2.0

I’m migrating a Web1.0 app and having trouble with Web2.0 and cookies. I have a socket on a login page that raises a custom event after a user successfully logs in that contains oauth2 values. In Web1.0 the following code worked to set/save browser cookies but does not in Web2.0 (2023r4 or 2024r1):

Sub Oauth2_Authentication_Results(access_token As String = "", expires_in As Integer = - 1, token_type As String = "", scope As String = "", refreshToken As String = "", httpStatus As Integer = - 1) Handles Oauth2_Authentication_Results
  var loginSession As Session = app.SessionForControl(self)
  //var webSessContext As New WebSessionContext(loginSession)
  if loginSession <> nil and httpStatus = 200 and access_token <> "" then
    #If DebugBuild then
      loginSession.Cookies.Set("userdata", access_token, DateTime.Now.AddInterval(0,0,1))
      loginSession.Cookies.Set("userdata", access_token, DateTime.Now.AddInterval(0,0,1), "", "", true, true)
    loginSession.accessToken = access_token
    loginSession.expiresIn = expires_in
    loginSession.tokenType = token_type
    loginSession.scope = scope
    loginSession.refreshToken = refreshToken
    MessageBox("Login Failed")
  end if
  Exception e As NilObjectException
End Sub

The code never breaks on an error and stepping through the code it seems to work, but the cookie is never set. Not even the simple commented out test cookie works.

Using: Session.Cookies.set("test","test") in the login page’s opening event does work. So I’m somehow not getting the correct session anymore. I’m not sure what changed between Web1.0 and Web2.0 regarding sessions.

Any help is appreciated. :grinning:

Hi Tony, this works for me (simple sample)…

Dim d As New Date
Session.Cookies.Set(cookie_name, cookie_value, d, "", "", False, False, WebCookieManager.SameSiteStrength.Off)

'Name As String, Expiration As DateTime = Nil, Domain As String = "", Path As String = "", Secure As Boolean = False, HTTPOnly As Boolean = False, 
'SameSiteStrength As WebCookieManager.SameSiteStrengths = WebCookieManager.SameSiteStrengths.Off

…and reading the cookie back…

Var the_cookie As String = Session.Cookies.Value(cookie_name)

William thanks for the suggestion.

Unfortunately from the custom event I can’t directly access the Session object. But you got me thinking again about how I could access the Session object directly in the Opening Event, so I moved the cookie setting code I had to the Opening Event of the page I would open after this code and the cookie set properly there.

I’m not sure why it doesn’t work where I had it in Web1.0, all the properties set fine just not the cookie.

My sanity is restored :smiley:. Thanks again for the help.

1 Like