簡體   English   中英

適用於iOS客戶端的Azure Active Directory刷新令牌

[英]Azure Active Directory Refresh Token for iOS client

我從門戶網站下載了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錯誤:

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/zh-cn/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.

            }];
        }
    }];
}

AAD需要進行刷新配置。 看到我關於這個主題的博客: https : //shellmonger.com/2016/04/13/30-days-of-zumo-v2-azure-mobile-apps-day-7-refresh-tokens/

如果您尚未配置刷新,則該刷新不可用。 一旦配置好刷新,然后在下次登錄時,您將獲得可以重新使用的刷新令牌。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM