C# Microsoft Graph API授权错误:无效访问群体
我知道这是一个很长的问题,但如果有人能和我分享他们的想法或经验,我会非常感激,因为我已经在这方面尝试了几天了。我有一个asp.net core 3.1 web API应用程序和一个asp.net core 3.1 MVC应用程序 两者都已在Azure AD中注册。API项目应该根据从MVC项目接收的请求负载创建日历事件。我正在按照来自的Microsoft说明进行操作 但一旦API项目对Microsoft Graph进行调用,就会失败,并出现以下错误: “代码”:“InvalidAuthenticationToken”,C# Microsoft Graph API授权错误:无效访问群体,c#,asp.net,azure,oauth-2.0,microsoft-graph-api,C#,Asp.net,Azure,Oauth 2.0,Microsoft Graph Api,我知道这是一个很长的问题,但如果有人能和我分享他们的想法或经验,我会非常感激,因为我已经在这方面尝试了几天了。我有一个asp.net core 3.1 web API应用程序和一个asp.net core 3.1 MVC应用程序 两者都已在Azure AD中注册。API项目应该根据从MVC项目接收的请求负载创建日历事件。我正在按照来自的Microsoft说明进行操作 但一旦API项目对Microsoft Graph进行调用,就会失败,并出现以下错误: “代码”:“InvalidAuthentic
“消息”:“访问令牌验证失败。访问群体无效。” 我在这里输入最小值是为了提供更多信息,但整个示例可以从下载 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
功能:
public static AuthenticationBuilder AddAzureAd(this AuthenticationBuilder builder, Action<AzureAdOptions> configureOptions)
{
builder.Services.Configure(configureOptions);
builder.Services.AddSingleton<IConfigureOptions<OpenIdConnectOptions>, ConfigureAzureOptions>();
builder.AddOpenIdConnect();
return builder;
}
下面是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);
}
}
我感谢您在这方面的想法和经验-再次感谢您的时间。看起来您的客户端应用程序正在获取Microsoft Graph API令牌:
options.Resource = "https://graph.microsoft.com";
访问令牌有一个访问群体(aud声明),用于指定它的API用途。
您的客户端应用程序需要使用API的客户端id或应用程序id URI作为资源。
通过这种方式,您可以获得一个用于API的访问令牌
那里的资源选项仅限于一个API。
如果您需要多个API的令牌,
您需要为AuthorizationCodeReceived设置事件侦听器,并使用MSAL.NET将授权代码交换为令牌。
我有一个这样做的示例应用程序:。
尽管此应用程序使用.NET Core 2.2和ADAL,但MSAL的一般方法类似。据我了解,您将请求从MVC发送到API,然后API调用Microsoft graph。同时,MVC和API应用程序受Azure AD保护。如果是这样,我建议您代表flow()使用。关于如何配置它,请参考我已经试过了,但还没有工作,但我会投票给你的答案,因为我已经从你的代码中学到了好东西。谢谢为了更加清晰,我还添加了获得令牌的代码。谢谢
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);
options.Resource = "https://graph.microsoft.com";