【发布时间】: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