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

标签 c# authentication azure azure-api-apps

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

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

但是,当使用 Microsoft 的 AzureCards 中的代码时控制台客户端示例(将浏览器添加到表单窗口并捕获 zumo token ),我无法获取用户的 aad token 并从 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 token 从测试程序复制到 postman 时,甚至会发生这种情况。程序失败, postman 成功返回。据我所知,他们发送了完全相同的请求。我可能会错过什么?

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

最佳答案

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"}]}

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

关于c# - 通过浏览器表单进行身份验证时从 Azure API 应用程序获取用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30533418/

相关文章:

c# - 在 WPF 中显示来 self 的网络摄像头的视频流?

c# - 计算通过 WCF 的包(数据契约(Contract))的大小?

azure - API 管理 URL 给出缺少订阅 key 问题

c# - 如何从Azure存储表中选择缺少字段的记录?

c# - Windows Azure 中的 APNS

c# - intArray 到 doubleArray,内存不足异常 C#

c# - 为什么串口接收到的数据为空?

Android - 为 Digest Auth 配置 Retrofit/Apache HttpClient

java - 如何为 Github API 验证基本 Java 程序

java - 使用 if 语句比较字符串