【问题标题】:C# .Net cookie has value in production but not on localhostC# .Net cookie 在生产中具有价值,但在 localhost 上没有
【发布时间】:2017-10-16 01:25:21
【问题描述】:

我有一个 cookie,其中包含用于后端处理的 tmp 文件名。

    try {
        var cookie = new HttpCookie("TargetExcelFile") {
            Name = "TargetExcelFile",
            Path = "/",
            Secure = true,
            Expires = DateTime.Now.AddDays(1d),
            Value = file
        };

        Response.Cookies.Add(cookie);
    } catch (Exception e) {
        throw new Exception("Create file cookie failed: " + e.Message);
    }

在生产中它工作正常,但在 localhost 上,浏览器中的值是空白的。我可以设置断点并查看 cookie 在Response.Cookies.Add(cookie); 行的两种情况下都有一个值。

localhost 上运行时,会创建 cookie,但在浏览器中没有任何值。

我认为Secure 标志可能是localhost 上的一个问题,所以我做了以下操作:

    try {
        var cookie = new HttpCookie("TargetExcelFile") {
            Name = "TargetExcelFile",
            Path = "/",
#if DEBUG
            Secure = false,
#else
            Secure = true,
#endif
            Expires = DateTime.Now.AddDays(1d),
            Value = file
        };

        Response.Cookies.Add(cookie);
    } catch (Exception e) {
        throw new Exception("Create file cookie failed: " + e.Message);
    }

...它没有帮助。

想法?

【问题讨论】:

    标签: c# .net cookies


    【解决方案1】:

    可能httpCookies 节点上的域属性设置为特定域。移除域名进行开发:

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

    此设置位于system.web 下的web.config 文件中。

    this question查看更多信息。

    【讨论】:

    • 谢谢大卫!像魅力一样工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    • 2022-01-20
    相关资源
    最近更新 更多