ABP Web 專案預設會使用自己的驗證,如果要使用 Azure AD 驗證登入的話,
可以依 How to Use the Azure Active Directory Authentication for MVC / Razor Page Applications ,
來啟用 Azure AD OpenIdConnect 驗證登入,以下就一步步地來實作它
1.註冊 App
到Portal Azure的Microsoft Entra ID中,
選點 Menu Manage -> App registrations 功能,點選「+ New registration」來註冊 App,

輸入 App 的名稱,選擇 account type(如果是公司內用請選擇Single tenant),
Redirect URI (optional)請選擇Web及輸入 Abp 網站的 URL,並加上signin-oidc(例如https://localhost:44392/signin-oidc),然後按下Register Button。
2.產生 Client Secrets
在已建立好的 App 中,選點 Menu Manage-> Certificates & secrets
點選Client secrets,按下「+ New client secret」,輸入 Description,選擇過期時間,再按下Add

3.Copy client secret 的值
將 client secret 的值 Copy 存下來,如下圖

4.Copy Directory (tenant) ID 及 Application (client) ID
點選 Menu Overview ,將 Directory (tenant) ID 及 Application (client) ID Copy 存下來
選選Endpoints可以知道要驗證的 URL,
如果是 Single Tenant ,OAuth 的 endpoint 會是 https://login.microsoftonline.com/{你的TenantId}/oauth2/v2.0,如果是多租戶,原本的 TenantId 則改為 common

5.在appsettings.json中加入 Azure AD 的 App 設定值,
在How to Use the Azure Active Directory Authentication for MVC / Razor Page Applications的ResponseType是使用CodeIdToken,這裡我們改用code
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"TenantId": "common", //如果是 Single Tenant 請改成 TenantId
"ClientId": "ClientId Value",
"ClientSecret": "ClientSecret Value",
"CallbackPath": "/signin-oidc",
"ResponseType": "code"
}
6.在 Web 專案的Module檔案中的ConfigureAuthentication Method 中設定啟用 Azure AD 驗證
在 Web 專案加入Microsoft.AspNetCore.Authentication.OpenIdConnect套件,並修改ConfigureAuthentication Method
ConfigureAuthentication Method,因為需要取得appsettings.json中的設定值,所以要多一個IConfiguration configuration的參數
private void ConfigureAuthentication(ServiceConfigurationContext context, IConfiguration configuration)
{
context.Services.AddAuthentication()
.AddOpenIdConnect("AzureOpenId", "Azure Active Directory OpenId", options =>

{


options.Authority = $"{configuration["AzureAd:Instance"]}{configuration["AzureAd:TenantId"]}/v2.0";


options.ClientId = configuration["AzureAd:ClientId"];


options.CallbackPath = configuration["AzureAd:CallbackPath"];


options.ClientSecret = configuration["AzureAd:ClientSecret"];


options.ResponseType = configuration["AzureAd:ResponseType"]!;


options.GetClaimsFromUserInfoEndpoint = true;


options.SaveTokens = true;


options.SignInScheme = IdentityConstants.ExternalScheme;


options.TokenValidationParameters = new TokenValidationParameters


{



ValidateIssuer = true,



IssuerValidator = (issuer, securityToken, validationParameters) =>



{




if (issuer.StartsWith($"{configuration["AzureAd:Instance"]}") && issuer.EndsWith("/v2.0"))




{





return issuer;




}




throw new SecurityTokenInvalidIssuerException($"Invalid issuer: {issuer}");



}


};

});
}
7.執行程式進行測試
在登入頁面中,點下Azure Active Directory OpenIdButton,
就會導到 Azure AD 進行登入驗證,如下圖

Azure AD 登入驗證後,會回到 Abp 程式,進行註冊來將 User 新增到資料庫之中,如下圖

回到首頁後,就可以看到使用者已登入到系統,如下圖

How to Use the Azure Active Directory Authentication for MVC / Razor Page Applications
When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.
評論