来自 Microsoft 的 Bearer Token 签名无效

问题描述 投票:0回答:1

我想使用 Azure 实现客户端凭据流。我在Azure中注册了两个应用程序(MyApi和MyClient)。 myClient 中的应用程序向 MS 发送 POST 请求以获取令牌。我使用此令牌向 Rest-API 服务器发送请求。答案始终是 401 Unauthorized - Baerer error="invalid_token" error_description="签名无效"。

这是我在 Azure 中的设置:

我的API

客户端ID:client_id_MyApi

租户 ID:tenant_id

应用程序 ID URI:api://MyApi

API 权限:Microsoft.Graph -> User.Read

公开 API -> 范围:api://MyApi/accessAsUser 应用程序角色:accessAsApplication

我的客户

客户端ID:client_id_MyClient 租户ID:tenant_id

Api 权限:Microsoft.Graph -> User.Read、MyApi -> accessAsApplication

REST-API 服务器的配置:

Program.cs
...
builder.Services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(o =>
{
    o.Audience = client id of MyClient;
    o.Authority = "https://login.microsoftonline.com/tenenat_id/";
    o.IncludeErrorDetails = true;
    

});
...

FooController.cs

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[Route("Foo")]
[HttpPost]
public async Task Foo()
{
    await Task.Delay(1000);
    Console.WriteLine("!!!!!!!!!!!!!!!");
}

请求后从 Microsoft 获取令牌:

https://login.microsoftonline.com/tenant_id/oauth2/v2.0/tokenHTTP/1.1

帖子正文:

grant_type=client_credentials&client_id=client_id_MyClient&client_secret=mysecret&scope=https://graph.microsoft.com/.default

c# azure rest bearer-token
1个回答
0
投票

注意:Microsoft Graph API 令牌并不需要验证,即 aud

https://graph.microsoft.com
,因为它不适用于应用程序。

https://login.microsoftonline.com/TenantID/oauth2/v2.0/token

client_id:ClientID
client_secret:ClientSecret
scope:https://graph.microsoft.com/.default
grant_type:client_credentials

enter image description here

当我解码访问令牌时,我收到无效签名错误

enter image description here

因此您可以避免验证 Microsoft Graph API 的访问令牌。

您可以验证您自己的 API 或应用程序的 访问令牌:

scope: api://ClientID/.default

enter image description here

现在我可以验证访问令牌了:

enter image description here

参考:

spring security - 使用 Azure AD 验证签名 - 堆栈内存溢出,作者:junnas

© www.soinside.com 2019 - 2024. All rights reserved.