c# - 安全 Owin Cookie http 和 https
问题描述
在我们的网站上进行了渗透测试,我们被告知该网站没有安全 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 文件中的代码。像这样运行网站时,cookies仍然不安全
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
解决方案
我的 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;
});
推荐阅读
- css - 底部有部分水平线的页脚
- python - 使用 pandas 和 Matplotlib 自定义日期时间索引的分组条形图
- javascript - 如何截断长于特定单词数的文本并添加省略号?
- database - 是否有一种简单的方法可以记录对 Access 表单的更改?
- java - SWT GC 围绕圆圈绘制和旋转文本
- sql - 如何在 Postgres 中为过去 90 天的记录创建索引 使 now() 不可变
- python - 根据通过该点的线的数量,在该点绘制具有颜色强度的最小-最大区域的线
- ios - 使用调度组时需要很长时间的异步函数
- ios - 当 VoiceOver 开启时,我的 UICollectionView 没有填充任何单元格
- javascript - 在点击或触摸时暂停事件,然后在 jQuery 中做一些工作后“重启”状态事件?