【发布时间】:2024-05-19 06:50:02
【问题描述】:
我正在构建一个由 SPA(角度)调用的 API。 用户使用 Azure AD 进行身份验证,API 使用 AzureAdBearer 进行身份验证。
public void ConfigureServices(IServiceCollection services)
{
[...]
services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme)
.AddAzureADBearer(options => Configuration.Bind("AzureActiveDirectory", options));
[...]
}
我需要调用 Graph API 并希望代表已连接的用户执行此操作。
我已经看到了这个问题:Accessing MS Graph from API on behalf of user currently signed in to separate web client 这看起来真的很像我尝试实施的解决方案。
我尝试在这里复制这个建议的解决方案:https://joonasw.net/view/azure-ad-on-behalf-of-aspnet-core
据我了解,我需要配置 JwtBearer 设置,但如果我已经拥有.AddAzureADBearer,我该如何调用.AddJwtBearer。
AddAzureADBearer 本身调用 AddJwtBearer(参见here)
我在我的 API 的应用注册中创建了一个秘密,并使用它来实现 IAuthenticationProvider 以代表用户获取令牌,但我收到错误 AADSTS65001。
我想这是因为我缺少将用户的令牌保存在缓存中的 AddJwtBearer 配置,但我不太确定。
我不太明白here这个问题上提出的解决方案。
更新
感谢Tony Ju's answer 我已经能够进行身份验证。但是当我调用图形 api 时,我得到 Authorization_RequestDenied 权限不足,无法完成操作。
我不知道为什么,但无法正确配置 Fiddler 以捕获对 Azure AD 的请求。我启用了 ADAL 日志并得到了这个
[Information][False]: 2020-01-09T09:11:12.1104926Z: AdalLoggerBase.cs: ADAL PCL.CoreCLR with assembly version '5.2.4.0', file version '5.2.4.0' and informational version '5.2.4' is running...
[Information][True]: 2020-01-09T09:11:12.1183853Z: AdalLoggerBase.cs: === Token Acquisition started:
Authority: https://login.microsoftonline.com/d6397071-8e3e-45d2-a2d6-36698acf0fea/
Resource: https://graph.microsoft.com
ClientId: [...]
CacheType: Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache (0 items)
Authentication Target: User
[Verbose][False]: 2020-01-09T09:11:12.1395851Z: AdalLoggerBase.cs: Username provided in user assertion - False
[Verbose][False]: 2020-01-09T09:11:12.5277838Z: AdalLoggerBase.cs: Loading from cache.
[Verbose][False]: 2020-01-09T09:11:12.5395969Z: AdalLoggerBase.cs: Looking up cache for a token...
[Information][False]: 2020-01-09T09:11:12.5467711Z: AdalLoggerBase.cs: No matching token was found in the cache
[Information][False]: 2020-01-09T09:11:12.5706903Z: AdalLoggerBase.cs: No matching token was found in the cache
[Verbose][False]: 2020-01-09T09:11:12.5736362Z: AdalLoggerBase.cs: Looking up cache for a token...
[Information][False]: 2020-01-09T09:11:12.5764248Z: AdalLoggerBase.cs: No matching token was found in the cache
[Verbose][False]: 2020-01-09T09:11:12.5804061Z: AdalLoggerBase.cs: Either a token was not found or an exception was thrown.
[Verbose][False]: 2020-01-09T09:11:12.5844932Z: AdalLoggerBase.cs: Cannot invoke the broker directly, may require install ...
[Verbose][False]: 2020-01-09T09:11:12.5891021Z: AdalLoggerBase.cs: Check and AcquireToken using broker
[Verbose][False]: 2020-01-09T09:11:12.5919785Z: AdalLoggerBase.cs: Broker invocation is NOT required
[Verbose][False]: 2020-01-09T09:11:12.9510499Z: AdalLoggerBase.cs: Storing token in the cache...
[Verbose][False]: 2020-01-09T09:11:12.9583491Z: AdalLoggerBase.cs: An item was stored in the cache
[Information][True]: 2020-01-09T09:11:12.9917987Z: AdalLoggerBase.cs: === Token Acquisition finished successfully. An access token was returned: Expiration Time: 09/01/2020 10:10:54 +00:00Access Token Hash: GtaeLmKsVSj82umwxVcgghW3/X/N2hKhaxyb7XBbBU0=
User id: 3b224748-42c5-406d-a0c0-e9c9f5238361
我使用此代码获取令牌:
var userAssertion = new UserAssertion(token, "urn:ietf:params:oauth:grant-type:jwt-bearer", user.upn);
var authContext = new AuthenticationContext(_aadOptions.Authority);
var clientCredential = new ClientCredential(_aadOptions.ClientId, _aadOptions.ClientSecret);
var result = await authContext.AcquireTokenAsync("https://graph.microsoft.com", clientCredential, userAssertion);
我尝试为 MSAL 而不是 ADAL 重写它,但没有成功。 图 api 请求所需的范围应该在初始用户登录中还是我们在代表用户请求令牌中扩展范围?
【问题讨论】:
-
详细的错误信息是什么? AADSTS65001 还不够。
标签: asp.net-core oauth-2.0 azure-active-directory