選單
GSS 技術部落格
在這個園地裡我們將從技術、專案管理、客戶對談面和大家分享我們多年的經驗,希望大家不管是喜歡或是有意見,都可以回饋給我們,讓我們有機會和大家對話並一起成長!
若有任何問題請來信:gss_crm@gss.com.tw
3 分鐘閱讀時間 (557 個字)

ABP Web專案整合 Azure Active Directory 驗證教學

ABP Web專案整合 Azure Active Directory 驗證教學

ABP Web 專案預設會使用自己的驗證,如果要使用 Azure AD 驗證登入的話,
可以依 How to Use the Azure Active Directory Authentication for MVC / Razor Page Applications ,
來啟用 Azure AD OpenIdConnect 驗證登入,以下就一步步地來實作它

實作

1.註冊 App
Portal AzureMicrosoft 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 ApplicationsResponseType是使用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

解決 Azure AD 登入時出現需要管理員核准的方法
使用 Dify 自動辨識非手寫圖片票據:以高鐵票為例

相關文章

 

評論

尚無評論
已經注冊了? 這裡登入
Guest
2025/08/21, 週四

Captcha 圖像