0

我正在尝试为 iOS 应用程序编写错误处理程序,并希望根据来自 Azure AD 令牌服务 (STS) 的 HTTP 错误代码以不同方式处理错误。例如,如果我收到 HTTP 429,我知道不重试请求,或者如果我收到 HTTP 500,我可能会选择重试。

如何从 ADAL Objective C 中提供的 AD 错误中提取此信息?

4

1 回答 1

2

我们不建议您在使用我们的库时直接使用 HTTP 错误代码。我们提供了 ADAL 错误代码,您应该使用这些代码来确定应用程序中的后续步骤。这些将 HTTP 错误和库可能抛出的其他错误都包装到您可以使用的一个 NSError 对象中。

您应该检查错误域ADAuthenticationErrorDomain,然后匹配错误代码,例如AD_ERROR_USER_INPUT_NEEDED

我们在 SDK 的头文件中维护此错误代码列表ADErrorCodes.h以及错误如何发生的描述:https ://github.com/AzureAD/azure-activedirectory-library-for-objc/blob/dev/ADAL/ src/public/ADErrorCodes.h

要在代码中捕获这些错误,请使用以下模式:

[authContext acquireTokenSilentWithResource:...
          completionBlock:^(ADAuthenticationResult *result) {

if (!result.error) {  

// normal program flow 

} else {

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

            // Handle user input needed error

       }

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

           // Handle user returned by the server 
           //does not match the the user identifier error

          }
}];
于 2017-06-07T23:34:25.657 回答