适用于iOS客户端的

时间:2016-10-25 10:01:04

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

我从我的门户网站下载了QuickStart的标准iOS应用程序,如下所示:

enter image description here

它是一个非常基本的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错误:


    Error Domain=com.Microsoft.MicrosoftAzureMobile.ErrorDomain Code=-1301 "The server returned an error." UserInfo={com.Microsoft.MicrosoftAzureMobile.ErrorResponseKey= { URL: https://xxxx-xxxxxx.azurewebsites.net/api/getUserProfileData } { status code: 401, headers {
        "Content-Length" = 0;
        Date = "Tue, 25 Oct 2016 09:18:57 GMT";
        Server = "Microsoft-IIS/8.0";
        "Set-Cookie" = "ARRAffinity=743d3a9e04f3c081f27c2f2c3af95d099f93dc60f14553bd4423b0abc62cfd33;Path=/;Domain=xxxx-xxxxxx.azurewebsites.net";
        "Www-Authenticate" = "Bearer realm=\"xxxx-xxxxxx.azurewebsites.net\"";
        "X-Powered-By" = "ASP.NET";
    } }, NSLocalizedDescription=The server returned an error., com.Microsoft.MicrosoftAzureMobile.ErrorRequestKey= { URL: https://xxxx-xxxxxx.azurewebsites.net/api/getUserProfileData }}

客户端命中的自定义节点脚本的日志给出了401.71未经授权的错误:


    2016-10-25T09:18:55  PID[35372] Warning     JWT validation failed: IDX10223: Lifetime validation failed. The token is expired.
    ValidTo: '10/25/2016 09:12:31'
    Current time: '10/25/2016 09:18:55'..
    2016-10-25T09:18:55  PID[35372] Information Sending response: 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 }}
     code - -1301
     domain - com.Microsoft.MicrosoftAzureMobile.ErrorDomain
    userInfo - {
        NSLocalizedDescription = "The server returned an error.";
        "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.

            }];
        }
    }];
}

1 个答案:

答案 0 :(得分:0)

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

如果尚未配置刷新,则不可用。配置刷新后,在您登录的NEXT时间,您将获得可以重复使用的刷新令牌。