【问题标题】:Azure Active Directory Refresh Token for iOS client适用于 iOS 客户端的 Azure Active Directory 刷新令牌
【发布时间】:2017-03-07 07:48:19
【问题描述】:

我从我的门户下载了一个来自 QuickStart 的标准 iOS 应用程序,如下所示:

它是一个非常基本的 iOS 应用,可与 Azure 后端进行通信,您可以在其中推送和拉取数据。

登录,退出

如果您要在门户中打开身份验证,则需要登录。您可以这样做:

[client loginWithProvider:@"windowsazureactivedirectory"
                   controller:controller
                     animated:YES
                   completion:^(MSUser *user, NSError *error) {
          // save data into your keychain
          // invoke various custom APIs
          // be able to pull and push data to your Easy Table
}];

注销也很简单:

[client logoutWithCompletion:^(NSError * _Nullable error) {

    if(!error) {
        [self clearCredentials];
        complete(nil);
    } else {
        DDLogError(@"%@", [error debugDescription]);
        complete(error);
    }

}];

刷新令牌

我的问题是令牌。我了解在使用 Active Directory 时,他们给您的令牌仅持续 1 小时。如果您要获得刷新令牌,您需要在 14 天内使用它。如果您确实在 14 天内使用它,那么之后最多可以使用 90 天。

因此,我等了一个小时,当我尝试使用客户端提取数据或调用 API 时,它会给我一个 401 错误:

错误域=com.Microsoft.MicrosoftAzureMobile.ErrorDomain 代码=-1301 “服务器返回错误。” UserInfo={com.Microsoft.MicrosoftAzureMobile.ErrorResponseKey= { URL: https://xxxx-xxxxxx.azurewebsites.net/api/getUserProfileData } { 状态码: 401, headers { “内容长度”= 0; 日期 =“2016 年 10 月 25 日星期二 09:18:57 GMT”; 服务器 = "Microsoft-IIS/8.0"; "Set-Cookie" = "ARRAffinity=743d3a9e04f3c081f27c2f2c3af95d099f93dc60f14553bd4423b0abc62cfd33;Path=/;Domain=xxxx-xxxxxx.azurewebsites.net"; "Www-Authenticate" = "承载领域=\"xxxx-xxxxxx.azurewebsites.net\""; "X-Powered-By" = "ASP.NET"; } }, NSLocalizedDescription=服务器返回错误,com.Microsoft.MicrosoftAzureMobile.ErrorRequestKey= { URL: https://xxxx-xxxxxx.azurewebsites.net/api/getUserProfileData }}

客户端点击我的自定义节点脚本的日志,给出 401.71 未经授权的错误:

2016-10-25T09:18:55 PID [35372] 警告 JWT 验证失败:IDX10223:生命周期验证失败。令牌已过期。 有效期至:'10/25/2016 09:12:31' 当前时间:'10/25/2016 09:18:55'.. 2016-10-25T09:18:55 PID[35372] 信息发送响应:401.71 Unauthorized

如果我要对我的表进行下拉以获取数据:

com.Microsoft.MicrosoftAzureMobile.ErrorRequestKey= { URL: https://xxx-xxxxx.azurewebsites.net/tables/TodoItem?$skip=0&$filter=(updatedAt%20ge%20datetimeoffset'2016-10-20T02%3A33%3A25 .607Z')&$orderby=updatedAt%20asc&__includeDeleted=true&$top=50 }} 代码 - -1301 域 - com.Microsoft.MicrosoftAzureMobile.ErrorDomain 用户信息 - { NSLocalizedDescription = "服务器返回错误。"; "com.Microsoft.MicrosoftAzureMobile.ErrorRequestKey" = " { URL: https://xxxx-xxxxxx.azurewebsites.net/tables/TodoItem?$skip=0&$filter=(updatedAt%20ge%20datetimeoffset'2016-10-20T02% 3A33%3A25.607Z')&$orderby=updatedAt%20asc&__includeDeleted=true&$top=50 }";

Microsoft Azure 在此处有一篇关于刷新令牌的小文章: https://azure.microsoft.com/en-us/blog/mobile-apps-easy-authentication-refresh-token-support/

我会使用 refreshUserWithCompletion: 方法,但它一直给我 403 错误。

因此,如果有人在 iOS 应用上成功刷新了他们的令牌,请发表评论。

谢谢!

更新:刷新作品,令牌仍将在 1 小时内到期

点击链接后,我的 refreshUserWithCompletion 方法起作用,并返回一个带令牌的有效 MSUser 对象。 但是,问题是这个令牌仍然只能使用 1 小时。我在下面粘贴我的刷新令牌代码。请推荐,谢谢!

if(!client.currentUser) {

    [client loginWithProvider:@"windowsazureactivedirectory"
                   controller:controller
                     animated:YES
                   completion:^(MSUser * user, NSError *error) {

        DDLogInfo(@"INITIAL TOKEN, userId: %@, token: %@", user.userId, user.mobileServiceAuthenticationToken);

        if(!error && user) {

            [client refreshUserWithCompletion:^(MSUser * _Nullable user, NSError * _Nullable error) {
                // user object now has new token for us to use. 
                // I'm assuming this is the refresh token.

                [self saveUserIntoKeyChain: user];
                // I can just load the userId and token from the keychain for future uses, and they all work. 
                // The only problem is that this token is only good for 1 hour.

            }];
        }
    }];
}

【问题讨论】:

    标签: ios azure azure-mobile-services access-token


    【解决方案1】:

    AAD 需要配置才能刷新。请参阅我关于该主题的博客:https://shellmonger.com/2016/04/13/30-days-of-zumo-v2-azure-mobile-apps-day-7-refresh-tokens/

    如果您尚未配置刷新,则它不可用。配置刷新后,下次登录时,您将获得一个可重复使用的刷新令牌。

    【讨论】:

    • 我浏览了那个链接,以及 Chris Gillum 的链接。我遵循了所有更改并将它们应用于我的配置。我现在可以使用 refreshUserWithCompletion 方法来刷新我的 MSUser 对象。它返回一个新的 MSUser 对象和一个新的令牌。但是,问题是这个令牌仍然只能使用 1 小时。每当我尝试在一小时后在该客户端上刷新我的 Easy Table(或调用其他 API)时,它仍然会给我一个 401 错误。我首先调用 loginWithProvider,然后调用 refreshUserWithCompletion。然后我将返回的用户对象保存到我的钥匙串中。
    • 捕获 401 错误 - 当你遇到一个错误时,请遵循涵盖所有情况的逻辑 - 缓存凭据、刷新令牌等。本书 C# 版本的第 2 章有我建议的顺序 - @ 987654322@ - 在 C#/.NET 中,但逻辑是一样的。
    猜你喜欢
    • 1970-01-01
    • 2021-08-13
    • 2017-02-12
    • 2019-08-25
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 2019-04-28
    相关资源
    最近更新 更多