【问题标题】:Secure flag not set to Cookies in .Net application.Net 应用程序中的安全标志未设置为 Cookie
【发布时间】:2018-02-24 05:36:26
【问题描述】:

我在我的 Web.Config 和 Global.asax.cs 文件中包含以下代码行。仍然当我在浏览器中使用开发人员工具时,我可以看到安全标志未设置为以下 Cookie。

还在我的 IIS 中配置了 SSLSettings(选中的复选框需要 SSL)。

我想为所有 Cookie 设置 Secure 属性,不仅要接收,还要设置已发送 cookie。请有任何建议。

在 Web.config 中:

<httpCookies requireSSL="true"/>

在 Global.asax.cs 中:

protected void Application_EndRequest(object sender, EventArgs e)
{
    if (Request.IsSecureConnection == true && HttpContext.Current.Request.Url.Scheme == "https")
    {
        Request.Cookies["ASP.NET_SessionID"].Secure = true;
        if (Request.Cookies.Count > 0)
        {
            foreach (string s in Request.Cookies.AllKeys)
            {
                Request.Cookies[s].Secure = true;
            }
        }

        Response.Cookies["ASP.NET_SessionID"].Secure = true;
        if (Response.Cookies.Count > 0)
        {
            foreach (string s in Response.Cookies.AllKeys)
            {
                Response.Cookies[s].Secure = true;
            }
        }
    }
}

在浏览器中:

【问题讨论】:

  • 在 Application_BeginRequest 或 Session_Start 中尝试相同的代码。
  • @G01 - 我已按照您的建议使用 Global.asax.cs 文件中的代码。但同样的结果。还有其他设置安全的方法吗?
  • 在 Web.config 文件中将 HttpOnlyCookie 设置为 true。
  • @G01 - 不走运。
  • 在 Web.config 文件中,检查 lockItem 或 lockattribute。如果是这样,请删除并在浏览器中再次检查。

标签: c# asp.net asp.net-mvc-3 cookies session-cookies


【解决方案1】:

有两种方法,web.config 中的一个 httpCookies 元素允许您打开 requireSSL,它只传输所有 cookie,包括 SSL 中的会话以及表单身份验证,但如果您在 httpcookies 上打开 SSL,您还必须打开它内部表单配置也是如此。

<form>
<httpCookies requireSSL="true" />

【讨论】:

    猜你喜欢
    • 2016-04-02
    • 2017-09-11
    • 2011-11-25
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 2016-02-18
    • 2015-02-08
    • 2016-09-11
    相关资源
    最近更新 更多