使用Postman在Chrome和x-zumo-auth标头中进行身份验证时,我的API应用会使用控制器中的以下代码成功识别用户的upn:

Runtime runtime = Runtime.FromAppSettings(Request);
EmaUserInfo user = runtime.CurrentUser;
TokenResult token = await user.GetRawTokenAsync("aad");
string upn = token.Claims[ClaimTypes.Upn];


但是,当使用Microsoft AzureCards Console Client示例中的代码(将浏览器添加到表单窗口并捕获zumo令牌)时,我无法获取用户的aad令牌并从API应用程序中获得以下错误:

Request to https://[gateway].azurewebsites.net/api/tokens?tokenName=aad&api-version=2015-01-14 GET failed BadRequest 400 (Bad Request)
{
  "status": 400,
  "source": "https://[gateway].azurewebsites.net/api/tokens?tokenName=aad&api-version=2015-01-14",
  "message": "Microservice '[API App]' only has permissions for token ''. Can't get token 'aad'"
}


有趣的是,当我设置一个断点并将zumo令牌从测试程序复制到邮递员中时,甚至会发生这种情况。程序失败,邮递员成功返回。据我所知,他们正在发送完全相同的请求。我可能会缺少什么?

编辑:我做了更多的测试,发现在隐身模式下执行Postman方法会产生相同的错误。这使我相信,不仅需要设置x-zumo-auth标头,而且还需要设置一些cookie以使用户正常工作。如果生成令牌,从apiapp网址中删除cookie并仅使用令牌发布请求,也会收到错误消息。

最佳答案

API应用程序应具有一个“ authentication”(身份验证)数组,该数组如下所示:

"authentication": [{"type": "aad"}]


您最终的apiapp.json应该是这样的:

{
"$schema": "http://json-schema.org/schemas/2014-11-01/apiapp.json#",
"id": "YourApiApps",
"namespace": "microsoft.com",
"gateway": "2015-01-14",
"version": "1.0.0",
"title": "YourApiApps",
"summary": "",
"author": "",
"endpoints": {
    "apiDefinition": "/swagger/docs/v1",
    "status": null
},
"authentication": [{"type": "aad"}]}


然后,请重新部署并再次检查。它应该解决此问题。

10-05 18:29