HelpASisterOut
HelpASisterOut

Reputation: 3185

Storing Value in HttpCookie

Using MVC.

Function Login(ByVal token As String) As ActionResult
        Using client As New WebClient
            Try
                Dim jsonResponse As String = client.DownloadString(URL & "/Getuser&token=" & token)
                Dim obj As UserInfo = Newtonsoft.Json.JsonConvert.DeserializeObject(Of UserInfo)(jsonResponse)

                Response.Cookies.Add(New HttpCookie("token", token))
                Response.Cookies.Add(New HttpCookie("user_id", obj.id))
                
                Return Json(obj)

            Catch ex As WebException 
                Return Content("ERROR")

            Catch ex As Exception 
                Return Content("ERROR")
            End Try

        End Using
    End Function
  1. I am sending a token to this function.

  2. Then Using this token to get the User Info from a certain API

  3. Then Storing this token in a HttpCookie

    All this has been working fine for almost a month, Until it stopped working.

When I debugged, token had a value, and it stored it in the HttpCookie, but when I called Request.Cookies("token").Value it returned ''

Any help would be appreciated.

I did a trace on the Token..

I am writing the parameter "token" in a file before storing it in the cookie. then I am writing the cookie Request.Cookies("token").Value in a file,

 Function Login(ByVal token As String) As ActionResult
    WriteToFile("TOKEN RECEIVED = ", token)

    Using client As New WebClient
        Try
            Dim jsonResponse As String = client.DownloadString(URL & "/Getuser&token=" & token)
            Dim obj As UserInfo = Newtonsoft.Json.JsonConvert.DeserializeObject(Of UserInfo)(jsonResponse)

            Response.Cookies.Add(New HttpCookie("token", token))
            Response.Cookies.Add(New HttpCookie("user_id", obj.id))
            WriteToFile("TOKEN COOKIE = ", Request.Cookies("token").Value)
            Return Json(obj)

        Catch ex As WebException 
            Return Content("ERROR")

        Catch ex As Exception 
            Return Content("ERROR")
        End Try

    End Using
End Function

it returns the following:

TOKEN RECEIVED = X132WEeRT3AASDV
TOKEN COOKIE = 

When I try to write both Request and Response Cookies:

WriteToFile("TOKEN COOKIE = ", Request.Cookies("token").Value)
WriteToFile("TOKEN COOKIE = ", Response.Cookies("token").Value)

Request.Cookies("token").Value Returns Empty String

Response.Cookies("token").Value Returns Actual Value

Upvotes: 1

Views: 1082

Answers (3)

Sha
Sha

Reputation: 2417

This is an old question on SO but for those interested, .NET team has created a new method to properly handle cookies.

This is available in .NET 4.7.1 and not earlier versions:

See under the section ASP.NET HttpCookie parsing here https://blogs.msdn.microsoft.com/dotnet/2017/09/13/net-framework-4-7-1-asp-net-and-configuration-features/

Upvotes: 0

zchpit
zchpit

Reputation: 3121

Maybe your cookie just expire after one month? When using cookies don't forget to set expiration date, and check web browser settings (example: if you are using "tor browser bundle" this can be an issue).

HttpCookie myCookie = new HttpCookie("UserSettings");
myCookie["Font"] = "Arial";
myCookie["Color"] = "Blue";
myCookie.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(myCookie);

https://msdn.microsoft.com/en-us/library/78c837bd%28v=vs.140%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

Upvotes: 1

pangular
pangular

Reputation: 709

Make sure you access the cookies only during the actual Http request. Maybe you have changed the way (or place where) you call this function.

Upvotes: 0

Related Questions