【问题标题】:Secure Cookies in ASP.NET not adding a secure FlagASP.NET 中的安全 Cookie 未添加安全标志
【发布时间】:2017-09-06 07:23:28
【问题描述】:

我创建了一个 asp.net 项目 创建了一个新的 HTTPCookie,我想在我的连接安全时为其添加一个安全标志 以下是我的代码

var responseCookie = new HttpCookie(Test)
        {
            HttpOnly = true,
            Value = asdasdhoi234
        };
        if (FormsAuthentication.RequireSSL && Request.IsSecureConnection)
        {
            responseCookie.Secure = true;
        }
       Response.Cookies.Set(Test);

但是cookie仍然不安全,我不明白这个问题。

在我的 httpsHeaders 中,它仍然没有显示我的安全 cookie 我的域是 https,但我的 cookie 仍然不安全。

【问题讨论】:

    标签: c# asp.net security cookies


    【解决方案1】:

    new HttpCookie constructor 将字符串作为参数。因此,我想您的 Test 是一个字符串。您需要在实际的 cookie 对象而不是字符串上设置 Secure 标志。试试这个:

    var responseCookie = new HttpCookie(Test)
    {
        HttpOnly = true,
        Value = "asdasdhoi234",
        Secure = FormsAuthentication.RequireSSL && Request.IsSecureConnection
    };
    Response.Cookies.Set(responseCookie);
    

    另外,请确保您的 web.config 包含 requireSSL 属性设置为 true,如 docs 所述:

    <authentication mode="Forms">
      <forms loginUrl="member_login.aspx"
        cookieless="UseCookies"
        requireSSL="true"
        path="/MyApplication" />
    </authentication>
    

    【讨论】:

    • 对不起,这是一个错字,我已经在实际对象上设置了标志,请检查我更新的问题
    • @ParshuramKalvikatte 请检查FormsAuthentication.RequireSSL 是否为真(需要在您的 web.config 中更新)
    • 我在 web.config 中没有此代码,但我在调试时不明白`FormsAuthentication.RequireSSL && Request.IsSecureConnection` 即使我的本地连接不安全,它也是如此。
    • 安全cookie是否需要添加FormsAuthentication验证?
    • @ParshuramKalvikatte 在我看来没有。只要会话本身是安全的,它应该没问题。因此Secure = Request.IsSecureConnection 应该足够了。但当然取决于您的需求。
    猜你喜欢
    • 2014-02-03
    • 2018-03-25
    • 2012-02-09
    • 1970-01-01
    • 2015-12-30
    • 2017-01-08
    • 2013-01-19
    • 2018-02-03
    • 2017-09-11
    相关资源
    最近更新 更多