我在我的microsoft.Identity.Web API项目中使用了.netcore包,该项目调用Graph来获取用户的目录对象。
在as文件中,下游api设置如下所示,
"DownstreamApi": {
"BaseUrl": "https://graph.microsoft.com/v1.0",
"Scopes": "Directory.Read.All"
},应用程序注册中设置了相关权限(Directory.Read.All)。
但是,即使我将“作用域”参数保留为空白,API也会给出目录对象。
因此,如果设置的格式如下,它仍然有效。那么,这个范围参数的需要是什么?
"DownstreamApi": {
"BaseUrl": "https://graph.microsoft.com/v1.0",
"Scopes": ""
},发布于 2022-01-18 14:02:25
范围声明可能没有反映在令牌中,因此您可能不会看到与指定范围有任何不同。
user_impersonation是最初存在于Azure中的每个Web应用程序或API的默认委托权限/scope。
如果需要,请确保在portal.And授予同意中添加所需的委托权限或应用程序权限。

在您的情况下,添加directory.read.all应用程序权限
例:我加了user.read

应用程序:
"DownstreamApi": {
"BaseUrl": "https://graph.microsoft.com/v1.0",
"Scopes": "user.read"
},在startUp.cs中
Public void ConfigureServices(IServiceCollection services)
{
string[] initialScopes = Configuration.GetValue<string>("DownstreamApi:Scopes")?.Split(' ');
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration)
// acquire a token to call a protected web API
.EnableTokenAcquisitionToCallDownstreamApi(initialScopes)
.AddMicrosoftGraph(Configuration.GetSection("DownstreamApi"))
.AddInMemoryTokenCaches();
//
//othercode
...
}在控制器中,我们需要指定作用域并发送到请求标头,以获得所需作用域的访问令牌。
参考资料:
如果client_credentials是授予类型,您可能需要在应用程序设置中使用https://graph.microsoft.com/.default作为作用域,这将给您为应用程序定义的权限。
"DownstreamApi": {
"BaseUrl": "https://graph.microsoft.com/v1.0",
"Scopes": "https://graph.microsoft.com/.default"
}尝试在请求中使用/token端点,而不是普通的
请参阅:
https://stackoverflow.com/questions/70729451
复制相似问题