【问题标题】:Office Add in not Authenticating to WebapiOffice 添加未对 Webapi 进行身份验证
【发布时间】:2016-05-03 09:37:47
【问题描述】:

当前在访问具有 autherize 属性并已注册到 Azure AD 的 Web api 控制器时遇到问题。目前,我已成功从 ADAL.JS 获取令牌,现在正尝试对测试 Webapi 进行 ajax 调用,该测试 Webapi 将为我的 Office-Addin 应用程序执行后端服务。我已经尝试了我将描述的场景。

第一个场景: 我创建了一个用于分隔 Web 应用程序条目的 Azure 管理门户,一个用于我的办公室 Add in 应用程序,以便我可以获取令牌,另一个用于我的 Web api,我可以将其锁定。然后,我允许我的 office-add 应用程序到我的 webapi,以便我的 office-Add in 与我的 web api 对话。我得到未经授权的 401 状态。

第二个场景:

由于我未经授权,我开始在我的 Azure 管理门户中创建一个新应用程序并使用 ADAL.js 获取令牌,但是当我对与我的办公室共享相同客户端编号的 webapi 进行相同的调用时 -插件应用程序我仍然获得 01 状态未授权。

不确定我做错了什么,似乎我已经尝试了两种可能的方法,但没有一个对我有用。这是我的 java 脚本

window.config = {
tenant: variables.azureAD,
clientId: variables.clientId,
postLogoutRedirectUri: window.location.origin,
endpoints: {
    sharePointUri: "https://" + tenant + ".sharepoint.com",
    ContentCenterApi: "https://localhost:44334"
},
cacheLocation: "localStorage"
};

  var authContext = new AuthenticationContext(config);

   var isCallback = authContext.isCallback(window.location.hash);
authContext.handleWindowCallback();

if (isCallback && !authContext.getLoginError()) {
window.location =     authContext._getItem(authContext.CONSTANTS.STORAGE.LOGIN_REQUEST);
}


function GetSharepointList(e) {
var authContext = new AuthenticationContext(config);
var user = authContext.getCachedUser();


if (!user) {
    authContext.login();
}

else {
    authContext.acquireToken(config.endpoints.sharePointUri, function (error, token) {

        if (error || !token) {
            console.log("ADAL error occurred: " + error);
            return;
        }
        else {
            var me = this;
            //var SiteUrl = config.endpoints.sharePointUri + "/sites/Apps_Dev/ER_TestSite/";
            $.ajax({

                url: 'https://localhost:44334/api/Values/Get',
                dataType: "json",
                headers: {
                    "Authorization": "Bearer " + token,
                    "accept": "application/json;odata=verbose",
                },
                success: function (data) {
                    handleData(data);
                }
            }).done(function (response) {
                console.log("Successfully fetched list from SharePoint.");
                // var items = response.d.results;
                //$("#contentPreview").append(items[0]);
            }).fail(function (error) {
                console.log("Fetching list from SharePoint failed.");
            });
        }

    });
};
}

【问题讨论】:

    标签: javascript ajax office365 adal office-js


    【解决方案1】:

    您调用的 API 是 “https://localhost:44334/api/Values/Get”,但获取令牌的资源 id 是“config.endpoints.sharePointUri”。它应该是您在 Azure AD 上注册的应用 URI。

    关于如何使用 Azure AD 保护 Web API。下面的文章可能会给你一些帮助。

    Protect a Web API using Bearer tokens from Azure AD.

    单页应用演示(adal.js):active-directory-angularjs-singlepageapp

    此示例演示了使用 ADAL for JavaScript 来保护基于 AngularJS 的单页应用程序,并通过 ASP.NET Web API 后端实现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-28
      • 2015-09-23
      • 2013-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-06
      相关资源
      最近更新 更多