首页 文章

无法使用承载令牌访问AAD安全Web API

提问于
浏览
3

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

我能够获得一个令牌,但是当我去使用令牌时,API应用程序似乎没有查看Authorization标头 . 它试图通过网络浏览器登录我 .

我的请求看起来像这样:

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>

我删除的持有者令牌,在反序列化时,其中包含我的信息,而不是我的消费应用程序 . 因此,它试图验证我,而不是使用持票人令牌进行身份验证 .

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

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

Update 2

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

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

2 回答

  • 6

    在弄清楚如何打开详细记录并通过它们之后,我想出了这个问题 .

    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(这是最大的罪犯,因为它正是我想做的不正确的信息)

  • 1

    你在用“ UseWindowsAzureActiveDirectoryBearerAuthentication ”吗?在Web API中,您应该使用它,将其添加到Startup Config中 . 如下:

    app.UseWindowsAzureActiveDirectoryBearerAuthentication(new WindowsAzureActiveDirectoryBearerAuthenticationOptions {Audience = ConfigurationManager.AppSettings [“ida:Audience”],Tenant = ConfigurationManager.AppSettings [“ida:Tenant”],});

    希望这对你有用,问候!

相关问题