我們正在分發一個 ASP.NET MVC 3.0 應用程式(C# 和 .NET 4.0),一些客戶在它上面有一個 iframe 用于一些自定義,但現在它已經停止作業。我認為這與我們所做的一些安全更改有關:
將“Content-Security-Policy”設定為“frame-ancestors 'self'”
強制 cookie 屬性(在 global.asax.cs 中導致 4.0 中沒有任何其他方法可以設定 samesite 屬性):
- SameSite:嚴格
- 安全:真
- HttpOnly:真
在洗掉“Content-Security-Policy”和 cookie 重寫規則之后,它就起作用了。但是,當我嘗試對(用戶/密碼)進行身份驗證時,不會發送身份驗證 cookie,并且我無法重寫它們,因為它們沒有隨請求一起提供。
我在網路請求/回應的 Cookie 選項卡上看到以下訊息:“通過 Set-Cookie 標頭設定 cookie 的嘗試被阻止,因為它具有“SameSite = Lax”屬性,但來自不是跨站點回應對頂級導航的回應”。
我讀過它與“最近的”瀏覽器安全更新和/或 Windows/ASP.NET 安全補丁有關,但經過一些研究,沒有解決方案適合我......

uj5u.com熱心網友回復:
我找到了解決方案:
升級到 .NET Framework 4.7.2:好的,我正在使用 4.0,我計劃今年升級到 4.8。我已經在一個分支上進行了測驗,并更改了該框架的一些新 cookie 屬性,它可以作業。
但是我有一個客戶在我們的網站上使用 iframe,遷移到 4.8 既不容易也不容易,所以我找到了 IIS 的 URL Rewrite 模塊的解決方案。我已經為內容安全策略添加了一條規則,以添加我的(他們的)iFrame 頁面主機。我已經關注了這些鏈接:
- https://www.petefreitag.com/item/850.cfm
- https://stackoverflow.com/a/60357945/803195
我為該客戶提供的最新版本的 web.config:
<rewrite>
<outboundRules>
<preConditions>
<!-- Checks User Agent to identify browsers incompatible with SameSite=None -->
<preCondition name="IncompatibleWithSameSiteNone" logicalGrouping="MatchAny">
<add input="{HTTP_USER_AGENT}" pattern="(CPU iPhone OS 12)|(iPad; CPU OS 12)" />
<add input="{HTTP_USER_AGENT}" pattern="(Chrome/5)|(Chrome/6)" />
<add input="{HTTP_USER_AGENT}" pattern="( OS X 10_14).*(Version/).*((Safari)|(KHTML, like Gecko)$)" />
</preCondition>
</preConditions>
<!-- Adds or changes SameSite to None for the session cookie -->
<!-- Note that secure header is also required by Chrome and should not be added here -->
<rule name="SessionCookieAddNoneHeader">
<!-- Use this regex if your OS/framework/app adds SameSite=Lax automatically to the end of the cookie -->
<match serverVariable="RESPONSE_Set-Cookie" pattern="((.*)(ASP.NET_SessionId[^=]*)(=.*))(?=SameSite)" />
<action type="Rewrite" value="{R:1}; SameSite=None; Secure=true" />
</rule>
<!-- Adds or changes SameSite to None for the session cookie -->
<!-- Note that secure header is also required by Chrome and should not be added here -->
<rule name="FormsCookieAddNoneHeader">
<!-- Use this regex if your OS/framework/app adds SameSite=Lax automatically to the end of the cookie -->
<match serverVariable="RESPONSE_Set-Cookie" pattern="((.*)(ASPXFORMSAUTH[^=]*)(=.*))(?=SameSite)" />
<action type="Rewrite" value="{R:1}; SameSite=None; Secure=true" />
</rule>
<rule name="RewriteContentSecurityPolicy">
<match serverVariable="RESPONSE_Content-Security-Policy" pattern="(.*)" />
<action type="Rewrite" value="{R:0} iframehost" />
</rule>
<!-- Removes SameSite=None header from all cookies, for most incompatible browsers -->
<rule name="CookieRemoveSameSiteNone" preCondition="IncompatibleWithSameSiteNone">
<match serverVariable="RESPONSE_Set-Cookie" pattern="(.*)(SameSite=None)" />
<action type="Rewrite" value="{R:1}" />
</rule>
</outboundRules>
</rewrite>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/455689.html
標籤:验证 asp.net-mvc-3 饼干
上一篇:在Linux下的檔案IO操作
