[英]Azure AD Group Claims from a powershell client
我正在嘗試使用 Azure AD 組聲明在對 REST 端點進行身份驗證之前檢查用戶是否是組的成員。 我首先克隆了這個項目: https : //github.com/Azure-Samples/active-directory-dotnet-webapp-groupclaims 。 但是,有兩個小的差異。
1) 我公開了一個 Web API REST 端點,它使用用戶令牌調用 GetGroups
var identity = (ClaimsIdentity)HttpContext.Current.User.Identity;
ClaimHelper.GetGroups(identity)
2) 我正在使用 powershell 客戶端而不是瀏覽器進行身份驗證。 以下是客戶端獲取令牌的方式:
#This function gets an azureADToken for our app
function Get-AzureADToken2 {
Add-Type -Path ".\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
$authority = "https://login.microsoftonline.com/microsoft.onmicrosoft.com"
$authContext = New-Object -TypeName Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext -ArgumentList ($authority);
$appIdURI = "https://mysite.azurewebsites.net"
$clientId = "6528ed9d9-1708-4b1b-851b-f773fa32f477"
$redirectURI = "https://mysite.azurewebsites.net/api"
#$platparam = new-object -TypeName Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformParameters -ArgumentList ( [Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior]::Auto)
$platparam = new-object -TypeName Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformParameters -ArgumentList ( [Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior]::Always)
$authContext.AcquireTokenAsync($appIdURI, $clientId, $redirectURI,$platparam);
}
當客戶端是瀏覽器時,一切都按預期工作,並且對 getgroups 的調用列出了適當的組。 但是,當使用 powershell 客戶端調用時,它會在此處拋出異常(假設令牌未從較早的瀏覽器會話中緩存,在這種情況下沒有異常):
AuthenticationResult result = await authContext.AcquireTokenSilentAsync(ConfigHelper.GraphResourceId, credential,
new UserIdentifier(claimsIdentity.FindFirst(Globals.ObjectIdClaimType).Value, UserIdentifierType.UniqueId));
{"Failed to acquire token silently as no token was found in the cache. Call method AcquireToken"}
在調試和手動檢查時,從瀏覽器和 PS 客戶端收到的聲明似乎幾乎相同。 我打開了 ADAL 診斷,但沒有任何用處。 有小費嗎?
此問題的根本原因是您的 Web API 的緩存中沒有訪問令牌。 代碼示例使用 OWIN Open ID 連接組件與 Azure AD 交互,它將在用戶登錄后獲取訪問令牌(檢查代碼Startup.Auth.cs#L39 )。 之后,它可以使用AcquireTokenSilentAsync
方法從緩存中靜默獲取令牌。
在您的方案中,您可以使用代表流來獲取 Azure AD Graph 的訪問令牌,使用 Web API 的訪問令牌。 有關此流程的更多詳細信息,您可以參考以下鏈接:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.