【问题标题】:MobileServices.web.js unauthorized api callMobileServices.web.js 未经授权的 api 调用
【发布时间】:2016-12-24 05:59:13
【问题描述】:

当我让我的 WinJS 应用程序处于休眠状态一段时间然后再返回它时,我单击了一个按钮,由于某种原因,我对后端的调用无法正常工作。

我从服务器收到“未经授权”错误。

如何修改 invokeApi 以便它重新验证用户或其他什么?

有没有人有任何使用 mobileservices.web.js 的经验以及如何让最终用户永久登录而无需重新验证自己?

谢谢。

client.invokeApi("getTopForumsTotal", {
    method: "post"
}).then(function (results) {
    // do something
}, function (error) {
    WinJS.log(error);
});

我使用 winjs mobileService 对用户进行身份验证。

client.login("microsoftaccount").done(function (results) {
    // Create a credential for the returned user.
    credential = new Windows.Security.Credentials.PasswordCredential("myapp", results.userId, results.mobileServiceAuthenticationToken);
    vault.add(credential);

    completeDispatcher();
}, function (error) {
    WinJS.log(JSON.stringify(error));
    errorDispatcher(error);
});

这是我用来刷新最终用户令牌的。

client._request("GET", "/.auth/refresh", null, null, {
    accept: "application/json",
    "ZUMO-API-VERSION": "2.0.0"
}, [], (error, response) => {
    if (!error) {
        var userObject = JSON.parse(response.responseText)

        if (userObject.authenticationToken) {
            client.currentUser.mobileServiceAuthenticationToken = userObject.authenticationToken;

            testCall().done(function (success) {
                if (success) {
                    credential = new Windows.Security.Credentials.PasswordCredential("myapp", userObject.user.userId, userObject.authenticationToken);
                    vault.add(credential);
                    authenticated = true;
                    completeDispatcher();
                }
                else errorDispatcher('testCall API does not exist');
            });
        }
        else errorDispatcher('no authentication token returned');
    }
    else errorDispatcher(error);
});

【问题讨论】:

  • 您是否使用 WinJS 创建 Windows/Windows Phone 应用程序?您如何验证您的移动应用程序?请提供更多信息以帮助我们更好地了解您的问题。
  • 我已经更新了我的答案。我遇到的问题是何时调用刷新令牌例程。它失败的地方是使用客户端对象对服务器进行 api 调用时。例如client.invokeApi、表调用或 client._request 调用。如果身份验证令牌过期,这些调用将失败,因此必须在此之前调用刷新。如何设置它以便在用户机器空闲并且他们返回时刷新令牌?
  • 关于如何使用刷新令牌我建议您参考this blog post
  • 嗨亚伦感谢您的链接。我去看看。

标签: javascript azure azure-mobile-services winjs


【解决方案1】:

我没有在每个 API 调用中都封装一个 promise,而是在客户端上合并了一个空闲例程,该例程在用户返回应用程序时刷新用户令牌,并在用户空闲时每 59 秒刷新一次令牌。

因此,出于所有强烈和目的,它们将始终具有有效的令牌或永久状态。

$(document).idle({
    onIdle: function () {
        // refresh user token
        if (User.Person !== null)
            User.Person.reauthenticate().done();
    },
    onActive: function () {
        // when the user returns refresh their token 1 more time
        if (User.Person !== null)
            User.Person.reauthenticate().done();
    },
    idle: 59000, // 59 seconds
    recurIdleCall: true // will keep refreshing every 59 seconds
});

https://github.com/kidh0/jquery.idle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 2017-04-22
    • 2019-08-13
    • 2013-08-22
    • 2019-03-07
    相关资源
    最近更新 更多