使用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当我调试时,token有一个值,并将它存储在HttpCookie中,但是当我调用Request.Cookies("token").Value时,它返回了''。
任何帮助都将不胜感激。
我在标记上做了追踪..。
在将参数"token“存储在cookie中之前,我正在将其写入文件中。然后我在一个文件中编写cookie Request.Cookies("token").Value,
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它返回以下内容:
TOKEN RECEIVED = X132WEeRT3AASDV
TOKEN COOKIE = 当我尝试同时编写请求和响应Cookies时:
WriteToFile("TOKEN COOKIE = ", Request.Cookies("token").Value)
WriteToFile("TOKEN COOKIE = ", Response.Cookies("token").Value)Request.Cookies(“令牌”).Value返回空字符串
Response.Cookies(“令牌”).Value返回实际值
发布于 2015-03-18 22:37:55
也许你的饼干在一个月后就过期了?在使用cookie时,不要忘记设置过期日期,并检查web浏览器设置(例如:如果您使用的是"tor browser bundle“,这可能是一个问题)。
HttpCookie myCookie = new HttpCookie("UserSettings");
myCookie["Font"] = "Arial";
myCookie["Color"] = "Blue";
myCookie.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(myCookie);发布于 2015-03-05 16:01:04
确保只在实际的Http请求期间访问cookie。也许您已经改变了调用此函数的方式(或位置)。
发布于 2017-10-31 21:31:40
这是一个老问题,但是对于那些感兴趣的人来说,.NET团队已经创建了一种新的方法来正确地处理cookie。
这可以在.NET 4.7.1中获得,而不是更早的版本:
参见ASP.NET HttpCookie解析 the https://blogs.msdn.microsoft.com/dotnet/2017/09/13/net-framework-4-7-1-asp-net-and-configuration-features/一节。
https://stackoverflow.com/questions/28874832
复制相似问题