【问题标题】:How to save and read Cookie in Asp.net Mvc如何在 Asp.net Mvc 中保存和读取 Cookie
【发布时间】:2020-12-20 19:06:25
【问题描述】:

我将我的 cookie 保存为以下代码:

public static void SetCookie(string key, string value, int expireDay = 1)
{
        var cookie = new HttpCookie(key , value);
        cookie.Expires = DateTime.Now.AddDays(expireDay);
        HttpContext.Current.Response.Cookies.Add(cookie);
}

保存时的cookie值如下:

读取 Cookie:

public static string GetCookie(string key)
{
        string value = string.Empty;

        var cookie = HttpContext.Current.Request.Cookies[key];

        if (cookie != null)
        {
            if (string.IsNullOrWhiteSpace(cookie.Value))
            {
                return value;
            }
            value = cookie.Value;
        }

        return value;
}

问题是读取cookie时,所有值都是空的如下图:

【问题讨论】:

  • 我刚刚意识到这不是一个 asp.net 核心问题。您能否改为检查控制器中 cookie 的值,看看是否有效?你能检查一下实际的http请求/响应,看看cookie是否真的被设置了吗?
  • 好的,我该怎么做? @galdin

标签: asp.net-mvc asp.net-mvc-4 cookies key-value


【解决方案1】:

cookie 的最大大小为 4kb。

【讨论】:

    【解决方案2】:

    其实你应该从请求头中读取cookies;没有回应!

    【讨论】:

      【解决方案3】:

      问题在这里:HttpContext.Current.Response.Cookies.AllKeys.Contains(key)。 您需要从请求中读取它。并将更改写入响应。

      这是一个更简单的工作示例,它只是打印“嘿!”,并在每个 GET 上附加一个感叹号:

          public class IndexModel : PageModel
          {
              public string CookieValue = "Hey!";
              private const string COOKIE_KEY = "HEY_COOKIE";
      
              public void OnGet()
              {
                  Request.Cookies.TryGetValue(COOKIE_KEY, out string? actualValue);
                  if (actualValue is not null) CookieValue = actualValue + "!";
      
                  // Only required since we're changing the cookie
                  // TODO: set the right cookie options
                  Response.Cookies.Append(COOKIE_KEY, CookieValue, new CookieOptions { }); 
              }
          }
      
      @page
      @model IndexModel
      
      <h1>@Model.CookieValue</h1>
      

      此外,在通过 HTTP 进行调试时,还可以查看 Chrome 的网络选项卡。

      【讨论】:

      • var cookie = HttpContext.Current.Response.Cookies.AllKeys.Contains(key) ? HttpContext.Current.Response.Cookies[key] : HttpContext.Current.Request.Cookies[key]; 这个条件同时检查 ResponseRequest @galdin
      • @MohammadSalmanian 它“检查”响应。 ? 之前的表达式是检查。
      • 更新帖子。var cookie = HttpContext.Current.Request.Cookies[key];@galdin
      【解决方案4】:

      您的问题是您使用了 HttpContext.Current.Response。取而代之的是,您应该在 SetCookie 方法中声明一个参数,如下所示:HttpContext 上下文,然后在控制器中,当您调用该方法时,您必须将 HttpContext 控制器属性作为参数发送。

      public static void SetCookie(HttpContext context, string key, string value, int expireDay = 1)
      {
              var cookie = new HttpCookie(key , value);
              cookie.Expires = DateTime.Now.AddDays(expireDay);
              context.Response.Cookies.Add(cookie);
      }
      

      在控制器中:

      SetCookie(HttpContext, yourKey,yourValue)
      

      您还应该像这样更改您的 GetCookie 方法:

      public static string GetCookie(HttpContext context,string key)
      {
              string value = string.Empty;
      
              var cookie = context.Request.Cookies[key];
      
              if (cookie != null)
              {
                  if (string.IsNullOrWhiteSpace(cookie.Value))
                  {
                      return value;
                  }
                  value = cookie.Value;
              }
      
              return value;
      }
      

      【讨论】:

        猜你喜欢
        • 2019-01-05
        • 1970-01-01
        • 2011-05-16
        • 1970-01-01
        • 2018-11-29
        • 2017-09-02
        • 2017-10-21
        • 2019-04-03
        • 2013-04-08
        相关资源
        最近更新 更多