刷新.net mvc后不存储Cookie

时间:2017-10-24 16:39:30

标签: c# cookies

我正在快速测试某些内容,但我无法将Cookie保存到浏览器中。当我在浏览器中检查cookie时,它们不存在。我的代码:

private void WriteCookie(string setting, string settingValue)
        {
            HttpCookie myCookie = new HttpCookie(setting);
            // Set the cookie value.
            myCookie.Value = settingValue;
            // Set the cookie expiration date.
            myCookie.Expires = DateTime.Now.AddDays(10);

            // Add the cookie.
            Response.Cookies.Add(myCookie);
        }

        private string ReadCookie(string setting)
        {
            HttpCookie myCookie = new HttpCookie(setting);
            myCookie = Request.Cookies[setting];

            // Read the cookie information and display it.
            if (myCookie != null)
                return myCookie.Value;

            return null;
        }

我在MVC操作#1中设置cookie:

    if(page != null)
    {
        WriteCookie("page", page.ToString());
    }

    if (pageDraft != null)
    {
        WriteCookie("pageDraft", pageDraft.ToString());
    }

然后我读了:

    if (ReadCookie("page") != null)
        page = Convert.ToInt32(ReadCookie("page"));

    if (ReadCookie("pageDraft") != null)
        pageDraft = Convert.ToInt32(ReadCookie("pageDraft"));

刷新页面时,我会丢失Cookie。为什么呢?

1 个答案:

答案 0 :(得分:0)

Probslem是web.config:

<httpCookies requireSSL="true" />

更改为:

<httpCookies  httpOnlyCookies="true" requireSSL="false" />