Http cookie没有添加到集合中

时间:2017-08-21 08:59:20

标签: asp.net-mvc

也许我做错了什么,但是在MVC中向this.Response cookie集合中添加了cookie之后:

   HttpCookie cookieRefreshToken = new HttpCookie("RefreshToken");
   cookieRefreshToken.Value = string.Format("{0}", refreshToken);
   cookieRefreshToken.Expires = new DateTime(2017, 12, 01);
   response.Cookies.Add(cookieRefreshToken);

当我尝试以不同的方式访问Cookie时 - 没有Cookie:

var cookie = request.Cookies.Get("RefreshToken").Value;

其中request是HttpRequestBase类型,响应类型为HttpResponseBase

我错了吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

   HttpCookie cookieRefreshToken = new HttpCookie("RefreshToken");
   cookieRefreshToken.Value = string.Format("{0}", refreshToken);
   cookieRefreshToken.Expires = new DateTime(2017, 12, 01);
    HttpContext.Current.Response.Cookies.Add(cookieRefreshToken);

 var cookies= HttpContext.Current.Request.Cookies["RefreshToken"];
 string values = cookies.Value;