IIS 安全响应头 以及 Cookie SameSite 整改说明
一、第一部分:批量添加全部安全 HTTP 响应头(图形界面操作)
包含你全套配置:X-XSS、XCTO、XFO、CSP、Referrer、X-Permitted、X-Download、HSTS
打开 IIS 管理器 → 左侧选中目标网站
功能视图双击 HTTP 响应头
右侧点击【添加】,逐条录入:
| 名称 | 值 |
|---|---|
| X-XSS-Protection | 1; mode=block |
| X-Content-Type-Options | nosniff |
| X-Frame-Options | SAMEORIGIN |
| Content-Security-Policy | script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: *.map.baidu.com *.bdimg.com bdimg.share.baidu.com res.wx.qq.com pucha.kaipuyun.cn dcs.conac.cn webservice.coolwei.com www.gov.cn zfwzgl.www.gov.cn; object-src 'self' |
| Referrer-Policy | origin-when-cross-origin |
| X-Permitted-Cross-Domain-Policies | master-only |
| X-Download-Options | noopen |
全部添加完成,无需重启 IIS,即时生效。
配套 web.config 自动生成片段(无需手动写,界面添加自动生成)
<customHeaders> <add name="X-XSS-Protection" value="1; mode=block" /> <add name="X-Content-Type-Options" value="nosniff" /> <add name="X-Frame-Options" value="SAMEORIGIN" /> <add name="Content-Security-Policy" value="script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: *.map.baidu.com *.bdimg.com bdimg.share.baidu.com res.wx.qq.com pucha.kaipuyun.cn dcs.conac.cn webservice.coolwei.com www.gov.cn zfwzgl.www.gov.cn; object-src 'self'" /> <add name="Referrer-Policy" value="origin-when-cross-origin" /> <add name="X-Permitted-Cross-Domain-Policies" value="master-only" /> <add name="X-Download-Options" value="noopen" /> </customHeaders>
二、第二部分:修复 Cookie 缺失 SameSite/HttpOnly/Secure(CSRF 告警)
IIS 图形界面配置 Cookie 安全属性
IIS 选中网站 → 双击 配置编辑器
Section 下拉选择
system.web/httpCookieshttpOnlyCookies = True
requireSSL = True(全站 HTTPS 才开,HTTP 改为 False)
sameSite = Lax(无 iframe 嵌套首选;需要第三方嵌入则改为 None)
Section 切换
system.web/sessionStatecookieSameSite = Lax / None(和上面保持一致)
若使用表单登录:Section 切换
system.web/authentication/formssameSite = Lax
requireSSL = True
右上角【应用】保存,自动写入 web.config
对应配置:xml
<system.web> <httpCookies httpOnlyCookies="true" requireSSL="true" sameSite="Lax"/> <sessionState cookieSameSite="Lax"/> </system.web>
Domain:不用配置,不写更安全,缩小 Cookie 作用域。
SameSite=None 强制要求:必须开启 requireSSL="true",否则浏览器丢弃 Cookie。
三、常见问题避坑
Strict-Transport-Security 仅 HTTPS 生效,HTTP 请求不会输出
SameSite=None 缺少 Secure → Cookie 失效无法登录
HTTP 站点配置 requireSSL=true → Cookie 不下发,登录异常
.NET4.7.2 以下图形界面无 SameSite 选项,只能用 URL 重写方案
Nginx 反向代理 + IIS 场景:安全头、Cookie 属性只在一端配置,避免重复追加报错
CSP 内包含
unsafe-inline、unsafe-eval,扫描会提示弱化安全,后续可逐步优化移除
四、完整整合后 web.config 参考模板
<configuration> <system.webServer> <httpProtocol> <customHeaders> <add name="X-XSS-Protection" value="1; mode=block" /> <add name="X-Content-Type-Options" value="nosniff" /> <add name="X-Frame-Options" value="SAMEORIGIN" /> <add name="Content-Security-Policy" value="script-src 'self' 'unsafe-inline' 'unsafe-eval' blob: *.map.baidu.com *.bdimg.com bdimg.share.baidu.com res.wx.qq.com pucha.kaipuyun.cn dcs.conac.cn webservice.coolwei.com www.gov.cn zfwzgl.www.gov.cn; object-src 'self'" /> <add name="Referrer-Policy" value="origin-when-cross-origin" /> <add name="X-Permitted-Cross-Domain-Policies" value="master-only" /> <add name="X-Download-Options" value="noopen" /> <add name="Strict-Transport-Security" value="max-age=31536000; includeSubDomains" /> </customHeaders> </httpProtocol> </system.webServer> <system.web> <httpCookies httpOnlyCookies="true" requireSSL="true" sameSite="Lax"/> <sessionState cookieSameSite="Lax"/> </system.web> </configuration>