注
これは、この記事の最新バージョンではありません。 現在のリリースについては、この記事の .NET 9 バージョンを参照してください。
Warnung
このバージョンの ASP.NET Core はサポート対象から除外されました。 詳細については、 .NET および .NET Core サポート ポリシーを参照してください。 現在のリリースについては、この記事の .NET 9 バージョンを参照してください。
Von Bedeutung
この情報はリリース前の製品に関する事項であり、正式版がリリースされるまでに大幅に変更される可能性があります。 Microsoft は、ここで提供される情報に関して明示的または黙示的な保証を行いません。
現在のリリースについては、この記事の .NET 9 バージョンを参照してください。
このチュートリアルでは、Active Directory フェデレーション サービス (ADFS) や Microsoft Entra ID などの WS-Federation 認証プロバイダーを使用した、ユーザーによるサインインを有効にする方法について説明します。 ここでは、Facebook、Google および外部プロバイダーの認証に関する記事で説明されている ASP.NET Core サンプル アプリを使用します。
ASP.NET Core アプリについては、Microsoft.AspNetCore.Authentication.WsFederation によって WS-Federation のサポートが提供されています。 Microsoft.Owin.Security.WsFederation から移植されたこのコンポーネントによって、そのしくみの多くが共有されています。 ただし、各コンポーネントには、いくつかの重要な点で相違があります。
既定では、新しいコンポーネントには次のような機能があります。
- 要求されていないログインを許可しません。 WS-Federation プロトコルのこの機能は、XSRF 攻撃に対して脆弱性があります。 ただし、
AllowUnsolicitedLogins
オプションを使用して有効にすることができます。 - サインイン メッセージに対する各フォーム ポストのチェックは行いません。
CallbackPath
に対する要求のみが確認されます。CallbackPath
の既定値は/signin-wsfed
ですが、RemoteAuthenticationOptions.CallbackPath クラスの継承された WsFederationOptions プロパティを介して変更することができます。 このパスは、SkipUnrecognizedRequests オプションを有効にすることで、他の認証プロバイダーとの共有が可能です。
Active Directory にアプリを登録する
Active Directory フェデレーション サービス (AD FS)
- ADFS 管理コンソールから、サーバーの証明書利用者信頼の追加ウィザードを開きます。
- データの手動入力を選択します。
証明書利用者の表示名を入力します。 この名前は、ASP.NET Core アプリでは重要ではありません。
Microsoft.AspNetCore.Authentication.WsFederation では、トークン暗号化がサポートされないため、トークン暗号化証明書を構成しないでください。
- アプリの URL を使用して、WS-Federation パッシブ プロトコルのサポートを有効にします。 アプリに対して、正しいポートであることを確認します。
注
これは HTTPS URL である必要があります。 開発時にアプリをホストする際、IIS Express による自己署名証明書の提供が可能です。 Kestrel では、証明書を手動で構成する必要があります。 詳細については、Kestrelドキュメント を参照してください。
ウィザードの残りの部分では [次へ ] をクリックし、最後に [閉じる ] をクリックします。
ASP.NET Core Identity では、名前 ID 要求が必要です。 [要求規則の編集] ダイアログから、いずれかを追加します。
- 変換要求規則の追加ウィザードで、既定の [LDAP 属性を要求として送信] テンプレートを選択したまま、 [次へ] をクリックします。 SAM-Account-Name LDAP 属性を名前 ID 送信要求にマッピングする規則を追加します。
- [要求規則の編集] ウィンドウで、 >[OK] の順にクリックします。
マイクロソフト エントラ ID
- Microsoft Entra ID テナントのアプリ登録ブレードに移動します。 [新しいアプリケーションの登録] をクリックします。
- アプリの登録の名前を入力します。 これは、ASP.NET Core アプリでは重要ではありません。
- サインオン URL としてアプリがリッスンする URL を入力してください:
-
[エンドポイント] をクリックして、フェデレーション メタデータ ドキュメントの URL をメモしておきます。 これは、WS-Federation ミドルウェアの
MetadataAddress
となります。
- 新しいアプリの登録に移動します。
[API の公開] をクリックします。 アプリケーション ID の URI について、 [設定]>[保存] の順にクリックします。
アプリケーション ID の URI をメモしておきます。 これは、WS-Federation ミドルウェアの
Wtrealm
となります。
ASP.NET Core Identity を指定せずに WS-Federation を使用する
WS-Federation ミドルウェアは、Identity なしで使用することができます。 次に例を示します。
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = WsFederationDefaults.AuthenticationScheme;
})
.AddWsFederation(options =>
{
options.Wtrealm = Configuration["wsfed:realm"];
options.MetadataAddress = Configuration["wsfed:metadata"];
})
.AddCookie();
services.AddControllersWithViews();
services.AddRazorPages();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = WsFederationDefaults.AuthenticationScheme;
})
.AddWsFederation(options =>
{
options.Wtrealm = Configuration["wsfed:realm"];
options.MetadataAddress = Configuration["wsfed:metadata"];
})
.AddCookie();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
WS-Federation を ASP.NET Core Identity の外部ログイン プロバイダーとして追加する
Microsoft.AspNetCore.Authentication.WsFederation への依存関係をプロジェクトに追加します。
WS-Federation を
Startup.ConfigureServices
に追加します。
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddAuthentication()
.AddWsFederation(options =>
{
// MetadataAddress represents the Active Directory instance used to authenticate users.
options.MetadataAddress = "https://<ADFS FQDN or AAD tenant>/FederationMetadata/2007-06/FederationMetadata.xml";
// Wtrealm is the app's identifier in the Active Directory instance.
// For ADFS, use the relying party's identifier, its WS-Federation Passive protocol URL:
options.Wtrealm = "https://localhost:44307/";
// For AAD, use the Application ID URI from the app registration's Overview blade:
options.Wtrealm = "api://bbd35166-7c13-49f3-8041-9551f2847b69";
});
services.AddControllersWithViews();
services.AddRazorPages();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddAuthentication()
.AddWsFederation(options =>
{
// MetadataAddress represents the Active Directory instance used to authenticate users.
options.MetadataAddress = "https://<ADFS FQDN or AAD tenant>/FederationMetadata/2007-06/FederationMetadata.xml";
// Wtrealm is the app's identifier in the Active Directory instance.
// For ADFS, use the relying party's identifier, its WS-Federation Passive protocol URL:
options.Wtrealm = "https://localhost:44307/";
// For AAD, use the Application ID URI from the app registration's Overview blade:
options.Wtrealm = "api://bbd35166-7c13-49f3-8041-9551f2847b69";
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
AddAuthentication(IServiceCollection, String) オーバーロードは、DefaultScheme プロパティを設定します。
AddAuthentication(IServiceCollection, Action<AuthenticationOptions>) オーバーロードを使用すると、認証オプションを構成でき、これを使用してさまざまな目的に既定の認証スキームを設定できます。 以降の AddAuthentication
への呼び出しで、前に構成した AuthenticationOptions プロパティがオーバーライドされます。
認証ハンドラーを登録する AuthenticationBuilder 拡張メソッドは、認証スキームごとに 1 回のみ呼び出すことができます。 スキームのプロパティ、スキーム名、および表示名の構成を可能にするオーバーロードが存在します。
WS-Federation でログインする
アプリを参照し、nav ヘッダーの [ログイン] リンクをクリックします。 WsFederation を使用してログインするオプションがあります。
プロバイダーとして ADFS を使用すると、ボタンは ADFS サインイン ページにリダイレクトされます。
プロバイダーとして Microsoft Entra ID を使用すると、ボタンは Microsoft Entra ID サインイン ページにリダイレクトされます。
新しいユーザーのサインインが成功すると、アプリのユーザー登録ページにリダイレクトされます。
ASP.NET Core