【问题标题】:Secure Owin Cookie http and https安全 Owin Cookie http 和 https
【发布时间】:2018-10-05 13:33:15
【问题描述】:

在我们的网站上进行了渗透测试,我们被告知该网站没有安全 cookie。这在 Http 和 Https 上都有。

我已经尝试了很多示例,但 cookie 仍然没有勾选安全选项。我不知道我哪里错了。

这是我在网络配置中尝试过的:

Solution 1 
<httpCookies requireSSL="true"/>

Solution 2
<httpCookies httpOnlyCookies="true" requireSSL="true" />

Solution 3
<httpCookies requireSSL="true" lockItem="true"   />

Solution 4
    <authentication mode="Forms">
    <forms loginUrl="Layout_Simple.cshtml" cookieless="UseCookies"  requireSSL="true"   path="/Account/Login" />
    </authentication>

在尝试了这些解决方案后,cookie 仍然不安全

然后我尝试了 Global.asax.cs 文件中的代码。像这样运行网站时,cookie 仍然不安全

 protected void Application_EndRequest(object sender, EventArgs e)
    {
       if (Response.Cookies.Count > 0)
       {
          foreach (string s in Response.Cookies.AllKeys)
         {
           if (s == FormsAuthentication.FormsCookieName || s.ToLower() == "_requestverificationtoken" || s.ToLower() == ".aspnet.applicationcookie") || s.ToLower() == "asp.net_sessionid"
           {
              Response.Cookies[s].Secure = true;                            Response.Cookies[FormsAuthentication.FormsCookieName].Secure = true;
             Response.Cookies["ASP.NET_SessionId"].Secure = true;
                    }
                }
            }
        }

我也尝试在 Startup.Auth.cs 文件中添加以下行,但这导致网站不再登录。

 app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
         AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
         LoginPath = new Microsoft.Owin.PathString("/Account/Login"),
         CookieSecure = Microsoft.Owin.Security.Cookies.CookieSecureOption.Always

【问题讨论】:

  • 首先检查服务器是否确实设置了该标志,通过检查浏览器开发工具中的响应 Set-Cookie 标头

标签: c# asp.net-mvc cookies owin


【解决方案1】:

我的 ASP.Net Core 3.1 Web API 遇到了同样的问题。它因违反“HttpOnlyCookies”和“InsecureCookie”而未能通过 Checkmarx 扫描(尽管它是一个没有 cookie 的 API)。我通过将其添加到 ConfigureServices 来修复它:

services.Configure<CookiePolicyOptions>(options =>
{
    options.HttpOnly = HttpOnlyPolicy.Always;
    options.Secure = CookieSecurePolicy.Always;
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 2016-03-14
    • 2014-06-08
    • 2011-08-16
    • 2018-10-19
    • 2011-01-18
    • 2012-01-02
    • 1970-01-01
    相关资源
    最近更新 更多