asp.mvc cookie在下一个请求(POST)中不返回

时间:2018-09-25 05:26:52

标签: asp.net-mvc caching cookies model-view-controller header

好的...所以我的控制器基类中有一个onActionExecuting帮助器。它只是将cookie添加到响应头中:

public static void SaveCookie(ECookieName cookieName, object data, HttpContextBase httpContext)
{
    string _cookieName = Enum.GetName(typeof(ECookieName), cookieName);

    if (httpContext == null)
        httpContext = new HttpContextWrapper(HttpContext.Current.ApplicationInstance.Context);

    string serialized = Newtonsoft.Json.JsonConvert.SerializeObject(data, new Newtonsoft.Json.JsonSerializerSettings()
    {
        PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects
    });

    HttpCookie cookie = new HttpCookie(_cookieName, serialized) {Path="/", HttpOnly=true, Secure=false, Expires=DateTime.Now.AddYears(1)};

    httpContext.Request.Headers.Remove(_cookieName);
    httpContext.Response.Headers.Remove(_cookieName);

    httpContext.Request.Headers.Add(_cookieName, serialized);
    httpContext.Response.Headers.Add(_cookieName, serialized);

    httpContext.Request.Cookies.Remove(_cookieName);
    httpContext.Request.Cookies.Add(cookie);
    httpContext.Request.Cookies[_cookieName].Path = "/";

    httpContext.Response.Cookies.Remove(_cookieName); // ensure it isn't in the response multiple times
    httpContext.Response.AppendCookie(cookie);
    httpContext.Response.Cookies[_cookieName].Path = "/";
}

当我到达第一页时,它会写入cookie,并且我可以在提琴手中看到它。该页面然后发布到身份验证操作...但是当它到达那里时...没有cookie .....所以我收集并保存在cookie中的数据不见了...应用崩溃。我很乐意任何投入。基本上,我存储着一串ID和其他一些我不能使用Session或Cache的杂项……这似乎是放置逻辑的地方,但它并没有达到我希望的那样。

0 个答案:

没有答案