iOS ADAL-Make silent call using refresh token

会有一股神秘感。 提交于 2019-12-04 19:33:13

When you use Microsoft's authentication libraries, you should always first check to see if there is a user in the cache that can be used for your resource before prompting the user to sign in. This allows us to check if the user had previously signed in to your app or if there are other apps that share state with your app that may have already asked the user to sign in elsewhere.

If the user is found, we will try to acquire a token without interrupting the user at all. Sometimes a user will have changed their password or done some other action that will require them to sign in again even if they have signed in to your app previously. This is what you are seeing. The library is telling you that for the user you are trying to acquire a token for, they need to sign in again to make something right.

In order to handle all these cases elegantly, we recommend that you use the pseudocode pattern of:

acquireTokenSilent()
(if error InteractiveAuthenticationRequired) {
    acquireTokenInteractively() }

The pattern first checks if a user you specify is available in the token cache. If it is, we then call the Azure Active Directory service to see if the Refresh token for that user is valid. If both of these are true, then the user is signed in silently. If the user isn't found or the server rejects the Refresh Token, then an error is sent from the library that indicates the user needs to sign in interactively.

In the above, you are doing this first part, but you aren't handling the case where the user needs to sign in if there is a problem.

The best way is to catch the error with a ADErrorCode of AD_ERROR_USER_INPUT_NEEDED

Here is a code sample on how to do this pattern.

// Here we try to get a token from the stored user information we would have from a successful authentication

    [authContext acquireTokenSilentWithResource:data.resourceId
                                       clientId:data.clientId
                                    redirectUri:redirectUri
                                         userId:data.userItem.userInformation.userId
                                completionBlock:^(ADAuthenticationResult *result) {
                                        if (!result.error)
                                            {

                                          completionBlock(result.tokenCacheStoreItem.userInformation, nil);
                                        } else {

                                                if ([result.error.domain isEqual:ADAuthenticationErrorDomain] && result.error.code == AD_ERROR_USER_INPUT_NEEDED) {

                                                    // Here we know that input is required because we couldn't get a token from the cache

                                                    [authContext acquireTokenWithResource:data.resourceId
                                                                                 clientId:data.clientId
                                                                              redirectUri:redirectUri
                                                                                   userId:data.userItem.userInformation.userId
                                                                          completionBlock:^(ADAuthenticationResult *result) {

                                                                              if (result.status != AD_SUCCEEDED)
                                                                              {
                                                                                  completionBlock(nil, result.error);
                                                                              }
                                                                              else
                                                                              {
                                                                                  data.userItem = result.tokenCacheStoreItem;
                                                                                  completionBlock(result.tokenCacheStoreItem.userInformation, nil);
                                                                              }
                                                                          }];
                                                } else {


                                                    completionBlock(nil, result.error);
                                                }
                                        }


                                }];

Keep in mind this code is very verbose. You will most likely want to have acquireTokenWithResource: a separate method that you could call with [self acquireTokenWithResource]

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!