在 ASP.NET Core 中WS-Federation使用者 ASP.NET Core

本文演示如何讓使用者使用 WS-Federation 身份驗證提供程式(如 Active Directory 聯合身份驗證服務 (ADFS) 或 Azure Active Directory (AAD) 。 它使用 ASP。NET Core Facebook、Google 和外部提供程式身份驗證中所述的示例應用。

對於 ASP。NET Core應用,WS-Federation Microsoft。AspNetCore。Authentication。WsFederation提供支援。 此元件從 Microsoft。Owin。Security。WsFederation 移植,並共享該元件的許多機制。 但是,元件在幾個重要方面有所不同。

預設情況下,新的中介軟體:

不允許未經請求的登入。 此協議的功能WS-Federation XSRF 攻擊。 但是,可以使用 選項啟用 AllowUnsolicitedLogins 它。

不檢查每個表單帖子中的登入訊息。 僅檢查對 的請求是否登入。預設為 ,但可以透過 CallbackPath CallbackPath /signin-wsfed WsFederationOptions類的繼承RemoteAuthenticationOptions。CallbackPath屬性進行更改。 可以透過啟用 SkipUnrecognizedRequests 選項與其他身份驗證提供程式共享此路徑。

將應用註冊到 Active Directory

Active Directory 聯合身份驗證服務

從 ADFS 管理控制檯

開啟

伺服器的“新增信賴方信任嚮導”:

在 ASP.NET Core 中WS-Federation使用者 ASP.NET Core

選擇手動輸入資料:

在 ASP.NET Core 中WS-Federation使用者 ASP.NET Core

輸入信賴方顯示名稱。 該名稱對於應用來說 ASP。NET Core很重要。

Microsoft。AspNetCore。Authentication。WsFederation 缺少對令牌加密的支援,因此請勿配置令牌加密證書:

在 ASP.NET Core 中WS-Federation使用者 ASP.NET Core

使用應用的 WS-Federation啟用對被動協議的支援。 驗證埠是否適用於應用:

在 ASP.NET Core 中WS-Federation使用者 ASP.NET Core

備註

這必須是 HTTPS URL。 IIS Express開發期間託管應用時,使用者可以提供自簽名證書。 Kestrel 需要手動證書配置。 有關 Kestrel 更多詳細資訊,請參閱文件。

單擊

嚮導

其餘部分中的“下一步”,最後

單擊"

關閉“。

ASP。NET Core Identity 需要名稱

ID

宣告。 從”編輯宣告

規則"對話方塊中新增一

個:

在 ASP.NET Core 中WS-Federation使用者 ASP.NET Core

在“

新增轉換宣告規則嚮導"中

,保留選中預設的”將

LDAP 屬性作為宣告

傳送“模板,然後單擊”下一

步"。

新增將

SAM-Account-Name

LDAP 屬性對映到

名稱 ID 傳出宣告

的規則:

在 ASP.NET Core 中WS-Federation使用者 ASP.NET Core

"編輯

>

宣告規則

**“視窗中單擊”完成確定**“。

Azure Active Directory

導航到 AAD 租戶的應用註冊邊欄選項卡。 單擊

"新建應用程式註冊":

在 ASP.NET Core 中WS-Federation使用者 ASP.NET Core

輸入應用註冊的名稱。 這對應用來說 ASP。NET Core很重要。

輸入應用偵聽的 URL 作為登入

URL:

在 ASP.NET Core 中WS-Federation使用者 ASP.NET Core

單擊

"終結點"

並記下

"聯合元資料文件

URL”。 這是WS-Federation的 的 MetadataAddress :

在 ASP.NET Core 中WS-Federation使用者 ASP.NET Core

導航到新的應用註冊。 單擊

"公開 API"。

單擊“應用程式 ID URI 集

""

>

儲存"。

記下應用程式

ID URI。

這是WS-Federation的 的 Wtrealm :

在 ASP.NET Core 中WS-Federation使用者 ASP.NET Core

在WS-Federation的情況下使用 ASP。NET Core Identity

無需 WS-Federation,即可使用中介軟體 Identity 。 例如:

C#

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(); });}

新增WS-Federation作為 的外部登入提供程式 ASP。NET Core Identity

將 Microsoft。AspNetCore。Authentication。WsFederation 上的依賴項新增到專案。

將WS-Federation 新增到 Startup。ConfigureServices :

C#

public void ConfigureServices(IServiceCollection services){ services。AddDbContext(options => options。UseSqlServer( Configuration。GetConnectionString(”DefaultConnection“))); services。AddDefaultIdentity(options => options。SignIn。RequireConfirmedAccount = true) 。AddEntityFrameworkStores(); services。AddAuthentication() 。AddWsFederation(options => { // MetadataAddress represents the Active Directory instance used to authenticate users。 options。MetadataAddress = ”https:///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();}

AddAuthentication (字串)過載設定DefaultScheme屬性。 AddAuthentication (Action)過載允許配置身份驗證選項,這些選項可用於為不同目的設定預設的身份驗證方案。 對的後續呼叫 AddAuthentication 重寫以前配置的AuthenticationOptions屬性。

對於註冊身份驗證處理程式的AuthenticationBuilder擴充套件方法,每個身份驗證方案只能呼叫一次。 存在允許配置方案屬性、方案名稱和顯示名稱的過載。

使用 WS-Federation

瀏覽到應用,然後單擊導航

標頭中的

”登入“連結。 可以選擇使用 WsFederation:

在 ASP.NET Core 中WS-Federation使用者 ASP.NET Core

使用 ADFS 作為提供程式,該按鈕重定向到 ADFS 登入頁

在 ASP.NET Core 中WS-Federation使用者 ASP.NET Core

將Azure Active Directory作為提供程式時,該按鈕將重定向到 AAD 登入頁

在 ASP.NET Core 中WS-Federation使用者 ASP.NET Core

新使用者成功登入會重定向到應用的使用者註冊頁:

在 ASP.NET Core 中WS-Federation使用者 ASP.NET Core