ASP.NET 4.7.2 VB MVC の SameSite Cookie サンプル
.NET Framework 4.7 には SameSite 属性のサポートが組み込まれていますが、元の標準に準拠しています。
修正プログラムが適用された動作では、値をまったく出力するのではなく、 の値None
を持つ属性を出力する の意味SameSite.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 マネージャーは、それをサポートする 各 コンポーネントに設定する必要があります。これには、CookieAuthentication と OpenIdConnectAuthentication が含まれます。
SystemWebCookieManager は、応答 Cookie の統合に関する 既知の問題 を回避するために使用されます。
サンプルの実行
サンプル プロジェクトを実行する場合は、最初のページでブラウザー デバッガーを読み込み、それを使用してサイトの Cookie コレクションを表示してください。
Edge と Chrome でこれを行うには、F12
キーを押してから Application
タブを選び、Storage
セクションの Cookies
オプションの下にあるサイト URL をクリックします。
上の図から、[Create SameSite Cookie]\(SameSite Cookie の作成\) ボタンをクリックすると、サンプルによって作成された Cookie の値が SameSite 属性値である Lax
ことがわかります。これは 、サンプル コードで設定された値と一致します。
制御しない Cookie のインターセプト
.NET 4.5.2 では、 Response.AddOnSendingHeaders
ヘッダーの書き込みをインターセプトするための新しいイベント が導入されました。 これは、クライアント コンピューターに返される前に Cookie をインターセプトするために使用できます。 このサンプルでは、ブラウザーが新しい sameSite の変更をサポートしているかどうかを確認する静的メソッドにイベントを接続し、サポートされていない場合は、新しい None
値が設定されている場合に属性を出力しないように Cookie を変更します。
イベントを処理し、独自のコードにコピーできる cookie sameSite
属性を調整する例については、イベントのフックの例については global.asax と SameSiteCookieRewriter.vb を参照してください。
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
していないブラウザーで 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
詳細情報
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示