【问题标题】:AppAuth iOS Token Exchange Problems Azure ADAppAuth iOS 令牌交换问题 Azure AD
【发布时间】:2017-09-11 18:28:44
【问题描述】:

我使用 Azure AD 租户为 Android 构建了一个 AppAuth 测试应用程序,它工作正常。现在,我正在尝试使用 iOS(Swift 4)进行相同的操作,但在尝试将访问代码交换为访问令牌时失败了。没有返回错误,我确实得到了一个 idToken 但没有 accessToken 或 refreshToken。没有其他错误。不知道发生了什么。如果没有访问令牌,我无法查询图表。我正在使用 Azure AD v2。以下是我的一些代码:

func appAuthAuthorize(authConfig: AuthConfig) {
    let serviceConfiguration = OIDServiceConfiguration(
        authorizationEndpoint: NSURL(string: authConfig.authEndPoint)! as URL,
        tokenEndpoint: NSURL(string: authConfig.tokenEndPoint)! as URL)

    let request = OIDAuthorizationRequest(configuration: serviceConfiguration, clientId: authConfig.clientId, scopes: [OIDScopeOpenID, OIDScopeProfile], redirectURL: NSURL(string: authConfig.redirectUri)! as URL, responseType: OIDResponseTypeCode, additionalParameters: nil)

    doAppAuthAuthorization(authRequest: request)
}


func doAppAuthAuthorization(authRequest: OIDAuthorizationRequest) {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate

    appDelegate.currentAuthorizationFlow = OIDAuthorizationService.present(authRequest, presenting: self, callback: {
        (authorizationResponse, error) in
        if (authorizationResponse != nil) {
            self.authState = OIDAuthState(authorizationResponse: authorizationResponse!)
            self.logMessage(message: "Got authorization code: \(String(describing: self.authState?.lastAuthorizationResponse.authorizationCode))")
            self.doTokenRequest()
        } else {
            self.authState = nil
            self.logMessage(message: "Authorization error: \(String(describing: error?.localizedDescription))")
        }
    })
}

func doTokenRequest() {
    let tokenExchangeRequest = authState?.lastAuthorizationResponse.tokenExchangeRequest()

    OIDAuthorizationService.perform(tokenExchangeRequest!) {
        tokenResponse, error in
        if tokenResponse == nil{
            self.logMessage(message: "Token exchange error: \(error!.localizedDescription)")
        } else {
            self.authState?.update(with: tokenResponse!, error: error)
            self.saveState()
            self.logMessage(message: "Received token response with accesToken: \(tokenResponse!.idToken!)")
            self.logMessage(message: "Received token response with accesToken: \(tokenResponse!.refreshToken!)")
            self.logMessage(message: "Received token response with accesToken: \(tokenResponse!.accessToken!)")
            self.retrieveUserProfile()
        }

        self.authState?.update(with: tokenResponse, error: error)
    }
}

【问题讨论】:

  • 您是否已使用 PowerShell 将应用程序注册到图形 API?我必须使用 v1.6 来执行此操作,但我不确定 v2 是否需要。
  • 不必这样做。我的 Android 应用程序和使用 MSAL 的 iOS 示例工作正常。它唯一无法正常工作的 AppAuth。
  • 啊,好的。我也许可以提供一个 OpenIdConnect 宁静的流程,但我从未使用过 MSAL。对不起。
  • 好吧,我正在使用适用于 iOS 的 AppAuth Google 库 (github.com/openid/AppAuth-iOS),如前所述,在尝试从 Azure AD 获取令牌时,我失败了。

标签: ios azure-ad-b2c appauth


【解决方案1】:

得到了答案。问题在于,根据授权服务器,必须使用为该服务器定义的 scopes。在上面的代码中,我使用了 OIDScopeOpenID 和 OIDScopeProfile 的默认 OpenId 范围。一旦我将其更改为 User.Read 的 Azure AD 范围,一切都开始正常工作。所以这里是函数 appAuthAuthorize 中代码的净变化:

func appAuthAuthorize(authConfig: AuthConfig) {
let serviceConfiguration = OIDServiceConfiguration(
    authorizationEndpoint: NSURL(string: authConfig.authEndPoint)! as URL,
    tokenEndpoint: NSURL(string: authConfig.tokenEndPoint)! as URL)

let request = OIDAuthorizationRequest(configuration: serviceConfiguration, clientId: authConfig.clientId, scopes: ["User.Read"], redirectURL: NSURL(string: authConfig.redirectUri)! as URL, responseType: OIDResponseTypeCode, additionalParameters: nil)

doAppAuthAuthorization(authRequest: request)

}

【讨论】:

  • 我收到“”令牌交换错误:操作无法完成。 OAuth 错误:invalid_grant" ,您是否遇到过类似的问题?
猜你喜欢
  • 2020-05-10
  • 1970-01-01
  • 1970-01-01
  • 2017-05-16
  • 1970-01-01
  • 2020-12-21
  • 1970-01-01
  • 2020-07-05
  • 1970-01-01
相关资源
最近更新 更多