Thursday, October 29, 2026
HomeiOS Developmentios - Azure AD B2C Didn't refresh token after enroll

ios – Azure AD B2C Didn’t refresh token after enroll

[ad_1]

I at the moment attempt to combine the Azure AD B2C (Electronic mail & Signal In with Apple). After I preview the login web page I enroll and it efficiently creates an account, proper after getting an entry token I name for refresh token as I would like that to make one other inner name, I fail refresh token with following error

However after I open the login web page from my App I get the next error:

Error Area=MSALErrorDomain Code=-50000 “(null)” UserInfo={MSALErrorDescriptionKey=Authority validation will not be supported for this sort of authority, MSALInternalErrorCodeKey=-42008, MSALCorrelationIDKey=2EFF58A4-24F2-4ABC-8D80-BC96EDD26AB7}

I’ve gone by way of a number of GitHub boards, checked Azure B2C documentation and still have tried the suggestion from one of many comparable questions from stackOverflow however I’ve had no luck.

Right here is the code I am utilizing:

func setup(coverage: CustomAuthenticationPolicy) {
        do {
            let siginPolicyAuthority = attempt self.getAuthority(forPolicy: coverage.stringPolicy)
            let editProfileAuthority = attempt self.getAuthority(forPolicy: self.kEditProfilePolicy)
            // Present configuration for MSALPublicClientApplication
            // MSAL will use default redirect uri once you present nil
            let pcaConfig = MSALPublicClientApplicationConfig(clientId: kClientID, redirectUri: kRedirectUri, authority: siginPolicyAuthority)
            pcaConfig.knownAuthorities = [siginPolicyAuthority]
            self.applicationContext = attempt MSALPublicClientApplication(configuration: pcaConfig)
            self.initWebViewParams()
        } catch {
            self.updateLoggingText(textual content: "Unable to create software (error)")
        }
    }

    non-public func initWebViewParams() {
        guard let rootViewController = rootViewControllerProvider?.rootViewController else {
            ErrorLog.sharedInstance.logError(sourceClass: String(describing: self), sourceFunc: #perform, message: "didn't init net view params for OIDC")
            return
        }
        self.webViewParamaters = MSALWebviewParameters(authPresentationViewController: rootViewController)
    }

public func refreshToken(_ completion: @escaping (String?)->()) {
        
        do {
            /**
             
             authority is a URL indicating a listing that MSAL can use to acquire tokens. In Azure B2C
             it's of the shape `https://<occasion/tfp/<tenant>/<coverage>`, the place `<occasion>` is the
             listing host (e.g. https://login.microsoftonline.com), `<tenant>` is a
             identifier inside the listing itself (e.g. a website related to the
             tenant, corresponding to contoso.onmicrosoft.com), and `<coverage>` is the coverage you want to
             use for the present person circulation.
             
             */
            
            let authority = attempt self.getAuthority(forPolicy: coverage.stringPolicy)
            
            /**
             
             Purchase a token for an present account silently
             
             - scopes: Permissions you need included within the entry token acquired
             within the outcome within the completionBlock. Not all scopes are
             gauranteed to be included within the entry token returned.
             - account: An account object that we retrieved from the appliance object earlier than that the
             authentication circulation can be locked right down to.
             - completionBlock: The completion block that can be known as when the authentication
             circulation completes, or encounters an error.
             */
            
            guard let thisAccount = attempt self.getAccountByPolicy(withAccounts: applicationContext.allAccounts(), coverage: coverage.stringPolicy) else {
                self.updateLoggingText(textual content: "There is no such thing as a account out there!")
                return
            }
            
            let parameters = MSALSilentTokenParameters(scopes: kScopes, account:thisAccount)
            parameters.authority = authority
            parameters.forceRefresh = true
            self.applicationContext.acquireTokenSilent(with: parameters) { (outcome, error) in
                if let error = error {
                    
                    let nsError = error as NSError
                    
                    // interactionRequired means we have to ask the person to sign-in. This normally occurs
                    // when the person's Refresh Token is expired or if the person has modified their password
                    // amongst different doable causes.
                    
                    if (nsError.area == MSALErrorDomain) {
                        
                        if (nsError.code == MSALError.interactionRequired.rawValue) {
                            
                            // Discover we provide the account right here. This ensures we purchase token for a similar account
                            // as we initially authenticated.
                            
                            guard let param = self.webViewParamaters else { return }
                            
                            let parameters = MSALInteractiveTokenParameters(scopes: self.kScopes, webviewParameters: param)
                            parameters.account = thisAccount
                            
                            self.applicationContext.acquireToken(with: parameters) { (outcome, error) in
                                
                                guard let outcome = outcome else {
                                    self.updateLoggingText(textual content: "Couldn't purchase new token: (String(describing: error ?? "No error informarion" as? Error))")
                                    return
                                }
                                
                                completion(outcome.accessToken)
                                self.updateLoggingText(textual content: "Entry token is (self.accessToken ?? "empty")")
                            }
                            return
                        }
                    }
                    
                    self.updateLoggingText(textual content: "Couldn't purchase token: (error)")
                    return
                }
                
                guard let outcome = outcome else {
                    
                    self.updateLoggingText(textual content: "Couldn't purchase token: No outcome returned")
                    return
                }
                
                
                completion(outcome.accessToken)
                self.accessToken = outcome.accessToken
                self.updateLoggingText(textual content: "Refreshing token silently")
                self.updateLoggingText(textual content: "Refreshed entry token is (self.accessToken ?? "empty")")
            }
        } catch {
            self.updateLoggingText(textual content: "Unable to assemble parameters earlier than calling purchase token (error)")
        }
    }

Observe:
I am calling refresh proper after I efficiently purchase entry token because of profitable enroll utilizing applicationContext.acquireToken(with: parameters) { [weak self] (outcome, error) in

Am I lacking something? Thanks

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments