身份验证上下文令牌缓存未定义

时间:2017-02-21 10:38:41

标签: cordova sharepoint sharepoint-online

我正在尝试将Sharepoint Online的静默身份验证实施到我的应用程序中。我添加了Office365连接服务。我正在使用一个测试页面,它只是用令牌和到期日填充2个文本框。我有以下代码(非静默身份验证),可以正常工作:

document.addEventListener('deviceready', function () {
$(document).ready(function () {
    var authContext = Microsoft.ADAL.AuthenticationContext;
    authContext.createAsync("https://login.microsoftonline.com/common/")
        .then(function (authContext) {
            authContext.acquireTokenAsync(
                "https://my.sharepoint.com",     // Resource URI
                "4be098f8-2184-4831-9ef7-3d17dbbef6a0",      // Client ID
                "http://localhost:4400/services/office365/redirectTarget.html"    // Redirect URI
            ).then(function (authResult) {
                $('#token').value = authResult.accessToken;
                $('#expire').value = authResult.expiresOn;
            }, function (err) {
                console.log(err);
            });
        }, function (err) {
            console.log(err);
        });
});
});

然后我尝试使用以下代码实现静默身份验证:

document.addEventListener('deviceready', function () {
$(document).ready(function () {
    var authContext = Microsoft.ADAL.AuthenticationContext;
    authContext.tokenCache.readItems().then(function (items) {
        if (items.length > 0) {
            authority = items[0].authority;
            authContext = new Microsoft.ADAL.AuthenticationContext(authority);
        }
        authContext.acquireTokenSilentAsync("https://my.sharepoint.com", "4be098f8-2184-4831-9ef7-3d17dbbef6a0").then
        (function (authResult) {
            $('#token').value = authResult.accessToken;
            $('#expire').value = authResult.expiresOn;
        },
        function (authContext) {
            authContext.acquireTokenAsync(
                "https://my.sharepoint.com",     // Resource URI
                "4be098f8-2184-4831-9ef7-3d17dbbef6a0",      // Client ID
                "http://localhost:4400/services/office365/redirectTarget.html"    // Redirect URI
            ).then(function (authResult) {
                $('#token').value = authResult.accessToken;
                $('#expire').value = authResult.expiresOn;
            }, function (err) {
                console.log(err);
            });
        }, function (err) {
            console.log(err);
        }
        )
    });
});
});

我在尝试读取令牌缓存时遇到错误,我将令牌缓存定义为未定义。我看到的每个样本都引用了令牌缓存,所以只是想知道它为什么会被定义?

1 个答案:

答案 0 :(得分:0)

我需要创建Async我的authContext对象才能访问tokenCache ... whoops