通过浏览器表单进行身份验证时从 Azure API 应用程序获取用户信息

Getting user info from Azure API App when authenticated via browser form

使用 Postman 在 Chrome 和 x-zumo-auth header 中进行身份验证时,我的 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 控制台客户端示例中的代码时(将浏览器添加到表单 window 并捕获 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 returns 成功。据我所知,他们发送的请求完全相同。我可能缺少什么?

编辑: 我进行了更多测试,发现在隐身模式下使用 Postman 方法会产生相同的错误。这让我相信,不仅 x-zumo-auth header 需要设置,还需要设置一些 cookie 以便用户正常工作。如果我生成令牌,我也会收到错误消息,从 apiapp url 和 POST 仅包含令牌的请求中删除 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"}]}

那么,请重新部署并再次检查。它应该可以解决问题。