我已在 Azure 中创建了一个 Web API。 此 Web API 在 SharePoint Online 中进行一些调用。一些 api 调用是代为的。
此 Web API 在 2018 年 5 月 1 日之前都可以正常工作 - 并且在 2018 年 5 月 1 日之前创建的旧应用服务上也可以正常工作。
一位微软员工表示:
As part of our security hardening efforts we do not allow id_token redemption for any application created after 2018-05-01 00:00:00.
在adal登录过程中,我得到了id_token。 id_token 的值与 access_token 相同:
当我调用 Web api 时,我会将此 token 作为不记名 token 发送。 Web API 获取此 token (字符串 accessToken)并启动方法“AcquireTokenAsync”:
var clientID = ConfigurationManager.AppSettings["ClientID"];
var clientSecret = ConfigurationManager.AppSettings["ClientSecret"];
var tenant = ConfigurationManager.AppSettings["Tenant"];
var appCred = new ClientCredential(clientID, clientSecret);
var authContext = new AuthenticationContext(
"https://login.microsoftonline.com/" + tenant);
var resource = new Uri(sharePointUrl).GetLeftPart(UriPartial.Authority);
var authResult = await authContext.AcquireTokenAsync(resource, appCred,
new UserAssertion(accessToken));
return authResult.AccessToken;
但是在调用“AcquireTokenAsync”的行中我收到了错误消息:
AADSTS240002: Input id_token cannot be used as 'urn:ietf:params:oauth:grant-type:jwt-bearer' grant
但是问题出在哪里呢?
最佳答案
问题在于,您在前端和后端使用相同的应用程序身份,而 MS 不允许您使用 Id token (由于前者,您在此处将其用作访问 token )来获取另一个访问 token 。
可能的解决方案:
- 注册另一个应用(前端JS应用应该是Native应用)
- 它应该使用 API 的客户端 ID 或应用 ID URI 作为资源来获取后端 API 的访问 token
- 然后 API 可以将访问 token 交换为另一个访问 token
如果这是一个 Multi-Tenancy 应用程序,迁移可能并不容易。 如果是单租户,那么一切都应该是可能的。 当然,您的前端应用程序应该需要调用 Azure AD 中的后端 API 的权限,并且应该授予该权限。
另一种解决方案是使用 ADAL.JS 在前端获取其他访问 token ,而不是在后端使用 on-behalf-of 并将其附加到除 Id token 之外的所有 API 请求。
关于c# - Web API 代表 adal id_token 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50835194/