我有一个使用 Azure AD (AAD) 保护的 API 应用程序。我还有一个用于消费应用程序的 AAD 应用程序,在消费应用程序中,我设置了访问 API 应用程序的权限。

我可以获得 token ,但是当我使用 token 时,API 应用程序似乎没有查看 Authorization header 。它试图通过网络浏览器让我登录。

我的请求是这样的:

    GET /api/ticketing/issueTopics HTTP/1.1
    Host: <removed>
    Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGc<rest is removed>
    Cache-Control: no-cache

This is what my Fiddler looks like.

我在 Postman 中得到的结果是一些 MS 重定向页面:
<html>
<head>
    <title>Working...</title>
</head>
<body>
    <form method="POST" name="hiddenform" action="<removed>/.auth/login/aad/callback">
        <input type="hidden" name="id_token" value="<bearer token removed>" />
        <input type="hidden" name="state" value="/api/ticketing/issueTopics" />
        <input type="hidden" name="session_state" value="<removed>" />
        <noscript>
            <p>Script is disabled. Click Submit to continue.</p>
            <input type="submit" value="Submit" />
        </noscript>
    </form>
    <script language="javascript">document.forms[0].submit();</script>
</body>

我删除的不记名 token 在反序列化时包含我的信息,而不是我的消费应用程序。因此,它试图对我进行身份验证,而不是使用不记名 token 进行身份验证。

任何想法如何解决这一问题?

更新 1

通过更新,我拉下了与我的消费应用程序相关的 servicePrincipal 数据,它清楚地表明消费应用程序应该能够与 API 应用程序对话。
    "oauth2Permissions": [{
        "adminConsentDescription": "Allow the application to access Ticketing API on behalf of the signed-in user.",
        "adminConsentDisplayName": "Access Ticketing API",
        "id": "<removed>",
        "isEnabled": true,
        "type": "User",
        "userConsentDescription": "Allow the application to access Ticketing API on your behalf.",
        "userConsentDisplayName": "Access Ticketing API",
        "value": "user_impersonation"
    }]

更新 2

我制作了一个控制台应用程序来尝试这种方式。我收到了 401(未经授权)。

一个有趣的观察是,如果我去 jwt.io 并粘贴我的 token ,它可以反序列化它,但它也说 token 无效(无效签名)。不确定这意味着什么。

最佳答案

在弄清楚如何打开详细的日志记录并仔细查看它们之后,我发现了这个问题。

MSDN 上的文档说将“资源”作为 App ID Uri 传递。但您实际上需要将客户端 ID 作为“资源”的值传递。一旦我改变了它,一切都完美无缺。

我在 LogFiles\Application 中的一个 txt 文件中找到了这个。

2016-07-12T15:48:39  PID[8584] Warning     JWT validation failed: IDX10214: Audience validation failed. Audiences: 'https://<removed>.azurewebsites.net'. Did not match:  validationParameters.ValidAudience: '0b61abb8-59...7-6046c22f9c92' or validationParameters.ValidAudiences: 'null'.

我正在查看的文档不正确:

https://msdn.microsoft.com/en-us/library/partnercenter/dn974935.aspx
https://msdn.microsoft.com/en-us/library/azure/dn645543.aspx(这是最大的冒犯者,因为它正是我想要用不正确的信息做的事情)

关于azure - 无法使用不记名 token 访问 AAD 安全 Web API,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38332313/

10-12 12:25
查看更多