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

jef*_*dio 1 c# authentication azure azure-api-apps

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

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

但是,当使用Microsoft的AzureCards控制台客户端示例中的代码(将浏览器添加到窗体窗口并捕获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'"
}
Run Code Online (Sandbox Code Playgroud)

有趣的是,这甚至发生在我设置断点并将zumo令牌从我的测试程序复制到邮递员时.程序失败,邮递员成功返回.据我所知,他们发送完全相同的请求.我能错过什么?

编辑:我已经做了一些测试,发现Postman方法在隐身模式下完成时会产生相同的错误.这让我相信它不仅仅是x-zumo-auth需要设置的标题,而且还需要设置一些cookie以便用户正常工作.如果我生成令牌,从apiapp url中删除cookie并仅使用令牌POST请求,我也会收到错误.

Pan*_*nos 6

API应用程序应该具有:authentication"数组,其类似于:

"authentication": [{"type": "aad"}]
Run Code Online (Sandbox Code Playgroud)

你的最终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"}]}
Run Code Online (Sandbox Code Playgroud)

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

  • 您必须重新启动网关. (3认同)