ASP.NET MVC 5 - Cookies being not created

本文关键字:being not created Cookies NET MVC ASP | 更新日期: 2023-09-27 18:29:01

我的控制器操作调用了一个处理cookie的私有方法。问题是根本没有创建cookie。我在SO上读了很多帖子,但我没有找到答案,因为我认为我在这段代码中处理cookie是正确的。

是否有任何关于cookie的web.config设置需要我检查?我还尝试了不同的浏览器。

我调试了代码,我可以看到cookie实际上是在代码中设置的,但当我加载页面并查看cookie时,cookie就不在了。

  private ABHomeModel HandleWhiteBoxCookie(ABHomeModel model)
        {
            var whiteBox = _whiteBoxService.GetActiveWhiteBox();
            if (model.WhiteBox != null)
            {
                const string cookieName = "whiteBox";
                var whiteBoxCookie = HttpContext.Request.Cookies.Get(cookieName);
                if (whiteBoxCookie != null)
                {
                    var displayedTimes = Convert.ToInt32(whiteBoxCookie.Value);
                    if (displayedTimes < 2)
                    {
                        displayedTimes++;
                        var cookie = new HttpCookie(cookieName, displayedTimes.ToString())
                        {
                            Expires = new DateTime().AddMonths(1),
                            Secure = false
                        };
                        HttpContext.Response.Cookies.Set(cookie);
                        ViewBag.IsWhiteBoxActive = true;
                    }
                    else
                    {
                        ViewBag.IsWhiteBoxActive = false;
                    }
                }
                else
                {
                    var cookie = new HttpCookie(cookieName, "1")
                    {
                        HttpOnly = true,
                        Domain = Request.Url.Host,
                        Expires = DateTime.Now.AddMonths(1),
                        Secure = false
                    };
                    HttpContext.Response.Cookies.Add(cookie);
                    ViewBag.IsWhiteBoxActive = true;
                }
                model.WhiteBox = whiteBox;
            }
            return model;
        }

ASP.NET MVC 5 - Cookies being not created

我的同事发现了这个问题。这是关于设置域。一旦我们删除了这条线:

Domain = Request.Url.Host,

Cookie已开始工作,目前正在创建中。

方法的完整更新代码:

private ABHomeModel HandleWhiteBoxCookie(ABHomeModel model)
        {
            var whiteBox = _whiteBoxService.GetActiveWhiteBox();
            if (whiteBox != null)
            {
                const string cookieName = "whiteBox";
                var whiteBoxCookie = HttpContext.Request.Cookies.Get(cookieName);
                if (whiteBoxCookie != null)
                {
                    var displayedTimes = Convert.ToInt32(whiteBoxCookie.Value);
                    if (displayedTimes < 2)
                    {
                        displayedTimes++;
                        var cookie = new HttpCookie(cookieName, displayedTimes.ToString())
                        {
                            HttpOnly = true,
                            Secure = false
                        };
                        HttpContext.Response.Cookies.Set(cookie);
                        ViewBag.IsWhiteBoxActive = true;
                    }
                    else
                    {
                        ViewBag.IsWhiteBoxActive = false;
                    }
                }
                else
                {
                    var cookie = new HttpCookie(cookieName, "1")
                    {
                        HttpOnly = true,
                        Expires = DateTime.Now.AddMonths(1),
                        Secure = false
                    };
                    HttpContext.Response.Cookies.Add(cookie);
                    ViewBag.IsWhiteBoxActive = true;
                }
                model.WhiteBox = whiteBox;
            }
            return model;
        }