在我们的网站上进行了渗透测试,我们被告知该网站没有安全的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>
然后我尝试了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
答案 0 :(得分:0)
我的ASP.Net Core 3.1 Web API遇到相同的问题。它因Checkmarx扫描失败而违反了“ HttpOnlyCookies”和“ InsecureCookie”(尽管它是一个没有cookie的API)。我通过将其添加到ConfigureServices来修复它:
services.Configure<CookiePolicyOptions>(options =>
{
options.HttpOnly = HttpOnlyPolicy.Always;
options.Secure = CookieSecurePolicy.Always;
});