共用方式為


ASP.NET 4.7.2 VB MVC 的 SameSite Cookie 範例

.NET Framework 4.7 具有SameSite屬性的內建支援,但它遵守原始標準。 修補的行為已變更 的意義 SameSite.None ,以使用 的值 None 發出 屬性,而不是完全不發出值。 如果您想要不要發出值,您可以將 Cookie 上的 屬性設定 SameSite 為 -1。

撰寫 SameSite 屬性

以下是如何在 Cookie 上寫入 SameSite 屬性的範例;

' Create the cookie
Dim sameSiteCookie As New HttpCookie("sameSiteSample")

' Set a value for the cookie
sameSiteCookie.Value = "sample"

' Set the secure flag, which Chrome's changes will require for SameSite none.
' Note this will also require you to be running on HTTPS
sameSiteCookie.Secure = True

' Set the cookie to HTTP only which is good practice unless you really do need
' to access it client side in scripts.
sameSiteCookie.HttpOnly = True

' Expire the cookie in 1 minute
sameSiteCookie.Expires = Date.Now.AddMinutes(1)

' Add the SameSite attribute, this will emit the attribute with a value of none.
' To Not emit the attribute at all set the SameSite property to -1.
sameSiteCookie.SameSite = SameSiteMode.None

' Add the cookie to the response cookie collection
Response.Cookies.Add(sameSiteCookie)

如果您要以英文以外的語言閱讀此文章,如果您想要以原生語言查看程式碼批註,請在此 GitHub 討論問題 中告訴我們。

會話狀態的預設 sameSite 屬性是在 中會話設定的 'cookieSameSite' 參數中設定 web.config

<system.web>
  <sessionState cookieSameSite="None">     
  </sessionState>
</system.web>

MVC 驗證

OWIN MVC Cookie 型驗證會使用 Cookie 管理員來啟用 Cookie 屬性的變更。 SameSiteCookieManager.vb是這類類別的實作,您可以複製到自己的專案中。

您必須確定您的 Microsoft.Owin 元件都已升級至 4.1.0 版或更新版本。 檢查您的 packages.config 檔案,以確保所有版本號碼都相符,例如。

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <!-- other packages -->
  <package id="Microsoft.Owin.Host.SystemWeb" version="4.1.0" targetFramework="net472" />
  <package id="Microsoft.Owin.Security" version="4.1.0" targetFramework="net472" />
  <package id="Microsoft.Owin.Security.Cookies" version="4.1.0" targetFramework="net472" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net472" />
  <package id="Owin" version="1.0" targetFramework="net472" />
</packages>

驗證元件必須設定為在啟動類別中使用 CookieManager;

Public Sub Configuration(app As IAppBuilder)
    app.UseCookieAuthentication(New CookieAuthenticationOptions() With {
        .CookieSameSite = SameSiteMode.None,
        .CookieHttpOnly = True,
        .CookieSecure = CookieSecureOption.Always,
        .CookieManager = New SameSiteCookieManager(New SystemWebCookieManager())
    })
End Sub

每個支援 Cookie 的元件 上都必須設定 Cookie 管理員,這包括 CookieAuthentication 和 OpenIdConnectAuthentication。

SystemWebCookieManager 可用來避免回應 Cookie 整合的 已知問題

執行範例

如果您執行範例專案,請在初始頁面上載入瀏覽器偵錯工具,並用它來檢視網站的 Cookie 集合。 若要在 Edge 和 Chrome 中執行此動作,請按 F12 下索引標籤, Application 然後按一下區段中選項 Storage 底下的 Cookies 網站 URL。

瀏覽器偵錯工具 Cookie 清單

您可以從上圖中看到,當您按一下 [建立相同網站 Cookie] 按鈕時,範例所建立的 Cookie 具有 的 SameSite 屬性值 Lax ,符合 範例程式碼中設定的值。

攔截您不控制的 Cookie

.NET 4.5.2 引進了攔截標頭寫入的新事件。 Response.AddOnSendingHeaders 這可用來在 Cookie 傳回至用戶端電腦之前攔截 Cookie。 在範例中,我們會將 事件連線到靜態方法,以檢查瀏覽器是否支援新的 sameSite 變更,如果尚未設定, None 則會變更 Cookie,不要發出屬性。

如需連結事件和SameSiteCookieRewriter.vb的範例,請參閱global.asax,以取得處理事件的範例,以及調整您可以複製到您自己的程式碼中的 Cookie sameSite 屬性。

Sub FilterSameSiteNoneForIncompatibleUserAgents(ByVal sender As Object)
    Dim application As HttpApplication = TryCast(sender, HttpApplication)

    If application IsNot Nothing Then
        Dim userAgent = application.Context.Request.UserAgent

        If SameSite.DisallowsSameSiteNone(userAgent) Then
            application.Response.AddOnSendingHeaders(
                Function(context)
                    Dim cookies = context.Response.Cookies

                    For i = 0 To cookies.Count - 1
                        Dim cookie = cookies(i)

                        If cookie.SameSite = SameSiteMode.None Then
                            cookie.SameSite = CType((-1), SameSiteMode)
                        End If
                    Next
                End Function)
        End If
    End If
End Sub

您可以以非常相同的方式變更特定的具名 Cookie 行為;下列範例會將預設驗證 Cookie 從 Lax 調整為 None 支援 None 值的瀏覽器,或在不支援 None 的瀏覽器上移除相同的Site 屬性。

Public Shared Sub AdjustSpecificCookieSettings()
    HttpContext.Current.Response.AddOnSendingHeaders(Function(context)
            Dim cookies = context.Response.Cookies

            For i = 0 To cookies.Count - 1
            Dim cookie = cookies(i)

            If String.Equals(".ASPXAUTH", cookie.Name, StringComparison.Ordinal) Then

                If SameSite.BrowserDetection.DisallowsSameSiteNone(userAgent) Then
                    cookie.SameSite = -1
                Else
                    cookie.SameSite = SameSiteMode.None
                End If

                cookie.Secure = True
            End If
            Next
        End Function)
End Sub

相關資訊

Chrome 更新

OWIN SameSite 檔

ASP.NET 文件

.NET SameSite Patchs