学习 javascript windows 商店应用令牌

adal javascript windows store app token

我有一个 javascript windows 商店应用程序,它通过 Microsoft.Preview.WindowsAzure.ActiveDirectory.Authentication 库使用 AAD 进行身份验证。它适用于托管在 Azure 中的 ASP.NET WebAPI。它工作正常,它提示用户登录(windows 登录服务),一旦登录用户就可以工作并且不会被要求再次登录。我发现的问题是,当用户有一段时间不使用应用程序时,我不确定但我认为是在 20 到 30 分钟之间,应用程序就会空闲。它不响应用户查询。我认为与 webApi 的通信丢失了,我不确定应用程序获取的令牌是否已过期或 Azure 本身切断了连接。

这就是我获得令牌的方式

var authcontext = new aal.AuthenticationContext(audience);
aal.DefaultTokenCache().clear();
return authcontext.acquireTokenAsync(resource, clientID, redirectUri.absoluteUri, "", null).then(function (result) {
     if (aal.AuthenticationStatus.succeeded == result.status) {
         accessToken = result.accessToken;
         return true;
     }
}

和 webApi 调用

var uri = new Windows.Foundation.Uri(apiUrl + apiName);
        var httpClient = new Windows.Web.Http.HttpClient();

        httpClient.defaultRequestHeaders.authorization =
            new Windows.Web.Http.Headers.HttpCredentialsHeaderValue("Bearer", accessToken);

        return httpClient.getAsync(uri).then(function (response) {
            if (response.isSuccessStatusCode == true)
                return response.content.readAsStringAsync().then(function (responseText) {
                    var array = JSON.parse(responseText);
                    return array;
                });
            else
            {
                var md = new Windows.UI.Popups.MessageDialog(response, "Error");
                md.showAsync();
            }
        });

我试过添加

<sessionTokenRequirement lifetime="96:00:00" /> 和 web.config 中的 <cookieHandler persistentSessionLifetime="60.0:0:0" requireSsl="true" /> 但仍然出现错误。

我尝试将 ADAL 新版本添加到项目中失败,因为我阅读了一些有关刷新令牌的内容,我正在研究它,但没有找到 windows 商店应用程序的任何示例。

关于问题的原因和解决方法的任何线索??

感谢您的宝贵时间。

您使用的库版本太旧且不受支持。此外,cookie 在 api 通信中没有任何作用。 我建议切换到最新的稳定版 ADAL。 Windows 使用 ADAL 存储样本:https://github.com/AzureADSamples/NativeClient-WindowsStore