I need to manage one client cookie and server cookies.
The client cookie is managed in the code via javascript(Cookies.js). The function is called in the Default.aspx with the onload event.
I’m trying now to manage some session cookies with one event in Global.asax.vb:
Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires upon attempting to authenticate the use
Dim context As HttpContext = HttpContext.Current
Dim cookies As HttpCookieCollection = context.Request.Cookies
For Each cookie As String In cookies.AllKeys
Try
Dim currentCookie As HttpCookie = cookies(cookie)
'Remove existent cookie
If currentCookie.Name.StartsWith("cookieName") Then
'Remove current cookie
Dim cookieToExpire As New HttpCookie(cookie)
cookieToExpire.Expires = DateTime.Now.AddDays(-1)
cookieToExpire.Path = "/PathName"
cookieToExpire.Domain = currentCookie.Domain
context.Response.Cookies.Set(cookieToExpire)
'Create cookie with hmac
Dim hmac As String = CalculateHMAC(currentCookie.Value, "Key")
Dim newCookie As New HttpCookie(cookie, currentCookie.Value & "|" & hmac)
newCookie.Expires = currentCookie.Expires
newCookie.Path = "/PathName"
newCookie.Domain = currentCookie.Domain
newCookie.HttpOnly = currentCookie.HttpOnly
newCookie.Secure = currentCookie.Secure
context.Response.SetCookie(newCookie)
End If
Catch ex As Exception
context.Response.Write("Error:" & ex.Message)
End Try
Next
End Sub
Notice that the implementation is successful, but the thing is, after writing this, the client cookie is not created now.
I see in devtools console the following error:
Uncaught ReferenceError: checkCookie is not defined
But without the code in Application_AuthenticateRequest this is running ok.
My wonder is the lifecycle of those events and if can coexists to apply the changes with both.