![](/img/trans.png)
[英]Microsoft Graph API: Getting error “Authorization_IdentityNotFound”
[英]Microsoft Graph API authorization error: Invalid Audience
我知道這是一個很長的問題,但如果有人能與我分享他們的想法或經驗,我將不勝感激,因為我已經在這方面工作了幾天,現在正在嘗試很多事情。 我有一個 asp net core 3.1 web API 應用程序和一個 ASP.NET Core 3.1 MVC 應用程序。
兩者都已在 Azure AD 中注冊。 API 項目應該根據從 MVC 項目接收到的請求負載創建日歷事件。 我正在按照此處鏈接中的 Microsoft 說明進行操作
但是一旦 API 項目對 Microsoft Graph 進行調用,它就會失敗並出現以下錯誤:
"code": "InvalidAuthenticationToken",
"message": "訪問令牌驗證失敗。無效的受眾。",
我在這里提供了更多信息,但可以從上面的鏈接下載整個示例。
ASP.NET 核心 MVC Startup.cs
:
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddAzureAd(options =>
{
Configuration.Bind("AzureAd", options);
AzureAdOptions.Settings = options;
})
.AddCookie();
ASP.NET 核心 MVC 項目AddAzureAd
function:
public static AuthenticationBuilder AddAzureAd(this AuthenticationBuilder builder, Action<AzureAdOptions> configureOptions)
{
builder.Services.Configure(configureOptions);
builder.Services.AddSingleton<IConfigureOptions<OpenIdConnectOptions>, ConfigureAzureOptions>();
builder.AddOpenIdConnect();
return builder;
}
ConfigureAzureOptions
:
public void Configure(string name, OpenIdConnectOptions options)
{
options.ClientId = _azureOptions.ClientId;
options.Authority = _azureOptions.Authority;
options.UseTokenLifetime = true;
options.CallbackPath = _azureOptions.CallbackPath;
options.RequireHttpsMetadata = false;
options.ClientSecret = _azureOptions.ClientSecret;
options.Resource = "https://graph.microsoft.com"; // AAD graph
// Without overriding the response type (which by default is id_token), the OnAuthorizationCodeReceived event is not called.
// but instead OnTokenValidated event is called. Here we request both so that OnTokenValidated is called first which
// ensures that context.Principal has a non-null value when OnAuthorizeationCodeReceived is called
options.ResponseType = "id_token code";
// Subscribing to the OIDC events
options.Events.OnAuthorizationCodeReceived = OnAuthorizationCodeReceived;
options.Events.OnAuthenticationFailed = OnAuthenticationFailed;
}
這是 API 項目中用於配置 Azure 選項的代碼:
private class ConfigureAzureOptions : IConfigureNamedOptions<JwtBearerOptions>
{
private readonly AzureAdOptions _azureOptions;
public ConfigureAzureOptions(IOptions<AzureAdOptions> azureOptions)
{
_azureOptions = azureOptions.Value;
}
public void Configure(string name, JwtBearerOptions options)
{
// options.Audience = _azureOptions.ClientId;
options.Authority = $"{_azureOptions.Instance}{_azureOptions.TenantId}";
// The valid audiences are both the Client ID(options.Audience) and api://{ClientID}
// --->>> I've changed this to also have "https://graph.micrososft.com" but no luck
options.TokenValidationParameters.ValidAudiences = new string[] { _azureOptions.ClientId, $"api://{_azureOptions.ClientId}" }; // <<--- I've changed this to "https://graph.micrososft.com" but no luck
// If you want to debug, or just understand the JwtBearer events, uncomment the following line of code
// options.Events = JwtBearerMiddlewareDiagnostics.Subscribe(options.Events);
}
public void Configure(JwtBearerOptions options)
{
Configure(Options.DefaultName, options);
}
}
這就是我從 MVC 項目獲得令牌的方式 - 權限是 api://client_id:
string userObjectID = User.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier")?.Value;
//AuthenticationContext authContext = new AuthenticationContext(AzureAdOptions.Settings.Authority, new NaiveSessionCache(userObjectID, HttpContext.Session));
AuthenticationContext authContext = new AuthenticationContext(AzureAdOptions.Settings.Authority);
ClientCredential credential = new ClientCredential(AzureAdOptions.Settings.ClientId, AzureAdOptions.Settings.ClientSecret);
感謝您對此的想法和經驗 - 再次感謝您的寶貴時間。
看起來您的客戶端應用正在獲取 Microsoft Graph API 令牌:
options.Resource = "https://graph.microsoft.com";
訪問令牌的受眾(aud 聲明)指定了它的用途 API。 您的客戶端應用程序需要使用 API 的客戶端 ID 或應用程序 ID URI 作為資源。 通過這種方式,您可以獲得一個適用於您的 API 的訪問令牌。
那里的資源選項僅限於一個 API。 如果需要多個 API 的令牌,則需要為 AuthorizationCodeReceived 設置事件偵聽器並使用 MSAL.NET 交換令牌的授權代碼。 我有一個示例應用程序可以執行此操作: https://github.com/juunas11/aspnetcore2aadauth/blob/97ef0d62297995c350f40515938f7976ab7a9de2/Core2AadAuth/Startup.cs#L58 。 這個應用程序雖然使用 .NET Core 2.2 和 ADAL,但使用 MSAL 的一般方法是相似的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.