Azure应用程序清单中的问题尝试使用Outlook Web加载项中的office-js-helpers进行身份验证

时间:2017-11-14 01:40:14

标签: azure-active-directory office-js outlook-web-addins office-js-helpers

我正在使用https://jsfiddle.net/2fyynf6p/来获取Outlook Web插件中的OAuth令牌,因此我可以将其用于带有EWS托管API的OAuthCredentials(其代码在Azure App Service中使用ASP.NET Web API)。

我已经在我的测试Office 365租户中配置了我的应用程序的应用程序注册(例如,mytenant.onmicrosoft.com,这不是托管Web应用程序的Azure订阅 - 如果这很重要)作为将oauth2AllowImplicitFlow设置为true的Native应用程序。我使用Native应用程序类型而不是Web / API应用程序绕过意外错误,指示我的应用程序需要管理员同意 - 即使没有请求应用程序权限 - 但这是另一个故事(也许我必须使用Native仍然 - 不是100%肯定) 。

我确保应用注册中的重定向URI(也称为回复网址)指向与Outlook加载项相同的页面(例如office-js-helpers)。

这是我的应用清单:

{ "appId": "a11aaa11-1a5c-484a-b1d6-86c298e8f250", "appRoles": [], "availableToOtherTenants": true, "displayName": "My App", "errorUrl": null, "groupMembershipClaims": null, "optionalClaims": null, "acceptMappedClaims": null, "homepage": "https://myapp.azurewebsites.net/MessageRead.html", "identifierUris": [], "keyCredentials": [], "knownClientApplications": [], "logoutUrl": null, "oauth2AllowImplicitFlow": true, "oauth2AllowUrlPathMatching": false, "oauth2Permissions": [], "oauth2RequiredPostResponse": false, "objectId": "a11aaa11-99a1-4044-a950-937b484deb8e", "passwordCredentials": [], "publicClient": true, "supportsConvergence": null, "replyUrls": [ "https://myapp.azurewebsites.net/MessageRead.html" ], "requiredResourceAccess": [ { "resourceAppId": "00000003-0000-0000-c000-000000000000", "resourceAccess": [ { "id": "e1fe6dd8-ba31-4d61-89e7-88639da4683d", "type": "Scope" } ] }, { "resourceAppId": "00000002-0000-0000-c000-000000000000", "resourceAccess": [ { "id": "311a71cc-e848-46a1-bdf8-97ff7156d8e6", "type": "Scope" }, { "id": "a42657d6-7f20-40e3-b6f0-cee03008a62a", "type": "Scope" } ] }, { "resourceAppId": "00000002-0000-0ff1-ce00-000000000000", "resourceAccess": [ { "id": "2e83d72d-8895-4b66-9eea-abb43449ab8b", "type": "Scope" }, { "id": "ab4f2b77-0b06-4fc1-a9de-02113fc2ab7c", "type": "Scope" }, { "id": "5eb43c10-865a-4259-960a-83946678f8dd", "type": "Scope" }, { "id": "3b5f3d61-589b-4a3c-a359-5dd4b5ee5bd5", "type": "Scope" } ] } ], "samlMetadataUrl": null }

我还确保将权限URL添加到我的加载项清单中:

<AppDomains>
 <AppDomain>https://login.windows.net</AppDomain>
 <AppDomain>https://login.microsoftonline.com</AppDomain>
</AppDomains>

这是我在使用office-js-helpers进行身份验证的加载项中使用的代码:

    // The Office initialize function must be run each time a new page is loaded.
Office.initialize = function(reason) {
    $(document).ready(function () {

        // Determine if we are running inside of an authentication dialog
        // If so then just terminate the running function

        if (OfficeHelpers.Authenticator.isAuthDialog()) {
            // Adding code here isn't guaranteed to run as we need to close the dialog
            // Currently we have no realistic way of determining when the dialog is completely
            // closed.
            return;
        }

        // Create a new instance of Authenticator
        var authenticator = new OfficeHelpers.Authenticator();

        authenticator.endpoints.registerAzureADAuth('a11aaa11-1a5c-484a-b1d6-86c298e8f250', 'mytenant.onmicrosoft.com');

        // Add event handler to the button
        $('#login').click(function () {
            $('#token', window.parent.document).text('Authenticating...');

            authenticator.authenticate('AzureAD', true)
                .then(function (token) {
                    // Consume and store the acess token
                    $('#token', window.parent.document).text(prettify(token));
                    authToken = token.access_token;
                })
                .catch(function (error) {
                    // Handle the error
                    $('#token', window.parent.document).text(prettify(error));
                });
        });
    });
};

现在,加载项中的代码可以正确登录用户并请求所需的权限,但是在单击应用程序授权步骤上的“接受”按钮后,将返回以下错误:

  

AADSTS50011:回复地址“https://mywebapp.azurewebsites.net/MessageRead.html”与为应用程序配置的回复地址不匹配:'a11aaa11-1a5c-484a-b1d6-86c298e8f250'。更多细节:未指定

每次单击“登录”按钮时都会返回错误(不再提示用户登录)。它从未检索过令牌。完整的身份验证URL为:

https://mywebapp.azurewebsites.net

我做错了什么?问题可能实际上是因为Web应用程序的主机名(重定向URI)与托管应用程序注册的Azure AD租户的域不匹配?如果是这样,我如何从托管没有Office 365或Exchange Online的Web应用程序的Azure订阅授予Exchange Online权限?我是否必须向我的测试Office 365租户添加Azure订阅,以便它还可以托管Web应用程序?

1 个答案:

答案 0 :(得分:2)

在您的应用清单中,我发现您使用https://myapp.azurewebsites.net/MessageRead.html作为replyUrls之一。 以下是您用来获得用户同意的网址。

https://login.windows.net/mydomain.onmicrosoft.com/oauth2/authorize?response_type=token&client_id=a11aaa11-484a-b1d6-86c298e8f250&redirect_uri=https%3A%2F%2Fmywebapp.azurewebsites.net&resource=https%3A%2F%2Fgraph.microsoft.com&state=982599964&nonce=3994725115

如果您观察到上面的网址,则提到redirect_urihttps://myapp.azurewebsites.net。但是redirect_uri应该与您在应用清单中提到的replyUrls中的至少一个匹配。

尝试在授权网址中将https://myapp.azurewebsites.net替换为https://myapp.azurewebsites.net/MessageRead.html

我已在下面的网址中更新了它们,如果您愿意,可以直接尝试以下网址。

https://login.windows.net/mydomain.onmicrosoft.com/oauth2/authorize?response_type=token&client_id=a11aaa11-484a-b1d6-86c298e8f250&redirect_uri=https%3A%2F%2Fmywebapp.azurewebsites.net%2FMessageRead.html&resource=https%3A%2F%2Fgraph.microsoft.com&state=982599964&nonce=3994725115