共用方式為


ASP.NET 4.7.2 VB WebForms 的 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 討論問題 中告訴我們。

表單驗證 Cookie 的預設 sameSite 屬性是在 表單驗證設定的 參數中 cookieSameSite 設定 web.config

<system.web>
  <authentication mode="Forms">
    <forms name=".ASPXAUTH" loginUrl="~/" cookieSameSite="None" requireSSL="true">
    </forms>
  </authentication>
</system.web>

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

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

.NET 的 2019 年 11 月更新已將表單驗證和會話 lax 的預設設定變更為 ,如同最相容的設定,不過,如果您將頁面內嵌到 iframe 中,您可能需要將此設定還原為 [無],然後新增如下所示的 攔截 程式碼,以根據瀏覽器功能來調整 none 行為。

執行範例

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

瀏覽器偵錯工具 Cookie 清單

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

攔截您不控制的 Cookie

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

如需連結事件和SameSiteCookieRewriter.cs的範例,請參閱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 行為;下列範例會將支援 值的瀏覽器 None 上的預設驗證 Cookie 從 Lax 調整為 None ,或移除不支援 None 的瀏覽器上的 sameSite 屬性。

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 更新

ASP.NET 文件

.NET SameSite 修補程式