【发布时间】: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