【发布时间】:2015-05-06 15:27:31
【问题描述】:
使用 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
-
我正在向这个函数发送一个令牌。
-
然后使用此令牌从某个 API 获取用户信息
-
然后将此令牌存储在 HttpCookie 中
这一切已经运行了将近一个月, 直到它停止工作。
当我调试时,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 =
当我尝试同时编写请求和响应 Cookie 时:
WriteToFile("TOKEN COOKIE = ", Request.Cookies("token").Value)
WriteToFile("TOKEN COOKIE = ", Response.Cookies("token").Value)
Request.Cookies("token").Value 返回空字符串
Response.Cookies("token").Value 返回实际值
【问题讨论】:
-
如果您使用 Firebug 等用于 Firefox 的调试工具,您可以轻松查看您为域设置的 cookie 以及该 cookie 的内容。这将帮助您确定 cookie 是否实际设置正确和/或是否重新读取 cookie 不正确。
-
@AllanS.Hansen 谢谢,虽然带有 token 的 cookie 是空的,但它显示带有 user_id 的 cookie 已满。我不知道为什么。
-
您能否编辑您的帖子,向我们展示您在文件中准确写入 cookie 的方式和时间
-
@Bombinosh 请检查编辑