【发布时间】:2018-06-06 17:56:08
【问题描述】:
我在 Xamarin.Forms 中使用客户端流身份验证,并试图弄清楚当身份验证令牌过期时如何处理。
我的代码:
初始登录时,用户使用本机 Facebook SDK 登录,我将 access_token 传递给 MobileServiceClient 以取回经过身份验证的用户。
var user = await client.LoginAsync(MobileServiceAuthenticationProvider.Facebook, token).ConfigureAwait(false);
然后我将用户的 UserId 和 MobileServiceAuthenticationToken 保存在本地设置中(使用 Xam.Plugins.Settings 插件)。
下次用户打开应用时,我从设置中设置用户并跳过手动登录:
if (!string.IsNullOrWhiteSpace(Settings.AuthToken) && !string.IsNullOrWhiteSpace(Settings.UserId))
{
client.CurrentUser = new MobileServiceUser(Settings.UserId);
client.CurrentUser.MobileServiceAuthenticationToken = Settings.AuthToken;
}
我的问题:
这很好用。但是,我知道MobileServiceAuthenticationToken 已过期。当到期日期到期时,我的应用程序会发生什么?如何在不要求用户重新登录 Facebook 的情况下刷新令牌?我已经尝试过 MobileServiceClient 的 RefreshUserAsync() 方法,但出现以下异常:
Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException:刷新失败,出现 400 错误请求错误。身份提供者不支持刷新,或者用户没有足够的权限登录。
有没有办法测试这个? (因为令牌到期时间为 3 个月后。)感谢您的帮助!
【问题讨论】:
-
Client-Flow 身份验证不允许刷新令牌,请阅读此 https://auth0.com/docs/api-auth/which-oauth-flow-to-use digitalocean.com/community/tutorials/an-introduction-to-oauth-2
-
@Johannes,感谢您提供的信息链接。我没有意识到 Facebook SDK 在幕后发生了这么多事情!因此,如果无法刷新访问令牌,如何防止我的用户不得不多次登录(令牌过期时)?我使用的大多数应用程序只要求我进行一次身份验证。
标签: authentication xamarin xamarin.forms azure-web-app-service azure-mobile-services