Authenticate Using Microsoft on Apple Platforms

You can let your users authenticate with Firebase using OAuth providers like Microsoft Azure Active Directory by integrating web-based generic OAuth Login into your app using the Firebase SDK to carry out the end to end sign-in flow.

Before you begin

To sign in users using Microsoft accounts (Azure Active Directory and personal Microsoft accounts), you must first enable Microsoft as a sign-in provider for your Firebase project:

  1. Add Firebase to your Apple project.
  2. In the Firebase console, open the Auth section.
  3. On the Sign in method tab, enable the Microsoft provider.
  4. Add the Client ID and Client Secret from that provider's developer console to the provider configuration:
    1. To register a Microsoft OAuth client, follow the instructions in Quickstart: Register an app with the Azure Active Directory v2.0 endpoint. Note that this endpoint supports sign-in using Microsoft personal accounts as well as Azure Active Directory accounts. Learn more about Azure Active Directory v2.0.
    2. When registering apps with these providers, be sure to register the *.firebaseapp.com domain for your project as the redirect domain for your app.
  5. Click Save.

Handle the sign-in flow with the Firebase SDK

To handle the sign-in flow with the Firebase Apple platforms SDK, follow these steps:

  1. Add custom URL schemes to your Xcode project:

    1. Open your project configuration: double-click the project name in the left tree view. Select your app from the TARGETS section, then select the Info tab, and expand the URL Types section.
    2. Click the + button, and add your Encoded App ID as a URL scheme. You can find your Encoded App ID on the General Settings page of the Firebase console, in the section for your iOS app. Leave the other fields blank.

      When completed, your config should look something similar to the following (but with your application-specific values):

      Screenshot of Xcode's custom URL scheme setup interface
  2. Create an instance of an OAuthProvider using the provider ID microsoft.com.

    Swift

    varprovider=OAuthProvider(providerID:"microsoft.com")
    

    Objective-C

    FIROAuthProvider*provider=[FIROAuthProviderproviderWithProviderID:@"microsoft.com"];
    
  3. Optional: Specify additional custom OAuth parameters that you want to send with the OAuth request.

    Swift

    provider.customParameters=[
    "prompt":"consent",
    "login_hint":"user@firstadd.onmicrosoft.com"
    ]
    

    Objective-C

    [providersetCustomParameters:@{@"prompt":@"consent",@"login_hint":@"user@firstadd.onmicrosoft.com"}];
    

    For the parameters Microsoft supports, see the Microsoft OAuth documentation. Note that you can't pass Firebase-required parameters with setCustomParameters. These parameters are client_id, response_type, redirect_uri, state, scope and response_mode.

    To allow only users from a particular Azure AD tenant to sign into the application, either the friendly domain name of the Azure AD tenant or the tenant's GUID identifier can be used. This can be done by specifying the "tenant" field in the custom parameters object.

    Swift

    provider.customParameters=[
    // Optional "tenant" parameter in case you are using an Azure AD
    // tenant. eg. '8eaef023-2b34-4da1-9baa-8bc8c9d6a490' or
    // 'contoso.onmicrosoft.com' or "common" for tenant-independent
    // tokens. The default value is "common".
    "tenant":"TENANT_ID"
    ]
    

    Objective-C

    // Optional "tenant" parameter in case you are using an Azure AD tenant.
    // eg. '8eaef023-2b34-4da1-9baa-8bc8c9d6a490' or
    // 'contoso.onmicrosoft.com' or "common" for tenant-independent tokens.
    // The default value is "common".
    provider.customParameters=@{@"tenant":@"TENANT_ID"};
    
  4. Optional: Specify additional OAuth 2.0 scopes beyond basic profile that you want to request from the authentication provider.

    Swift

    provider.scopes=["mail.read","calendars.read"]
    

    Objective-C

    [providersetScopes:@[@"mail.read",@"calendars.read"]];
    

    To learn more, refer to the Microsoft permissions and consent documentation.

  5. Optional: If you want to customize the way your app presents the SFSafariViewController or UIWebView when displaying the reCAPTCHA to the user, create a custom class that conforms to the AuthUIDelegate protocol, and pass it to credentialWithUIDelegate.

  6. Authenticate with Firebase using the OAuth provider object.

    Swift

    // Replace nil with the custom class that conforms to AuthUIDelegate
    // you created in last step to use a customized web view.
    provider.getCredentialWith(nil){credential,errorin
    iferror!=nil{
    // Handle error.
    }
    ifcredential!=nil{
    Auth().signIn(with:credential){authResult,errorin
    iferror!=nil{
    // Handle error.
    }
    // User is signed in.
    // IdP data available in authResult.additionalUserInfo.profile.
    // OAuth access token can also be retrieved:
    // (authResult.credential as? OAuthCredential)?.accessToken
    // OAuth ID token can also be retrieved:
    // (authResult.credential as? OAuthCredential)?.idToken
    }
    }
    }
    

    Objective-C

    [providergetCredentialWithUIDelegate:nil
    completion:^(FIRAuthCredential*_Nullablecredential,NSError*_Nullableerror){
    if(error){
    // Handle error.
    }
    if(credential){
    [[FIRAuthauth]signInWithCredential:credential
    completion:^(FIRAuthDataResult*_NullableauthResult,NSError*_Nullableerror){
    if(error){
    // Handle error.
    }
    // User is signed in.
    // IdP data available in authResult.additionalUserInfo.profile.
    // OAuth access token can also be retrieved:
    // ((FIROAuthCredential *)authResult.credential).accessToken
    // OAuth ID token can also be retrieved:
    // ((FIROAuthCredential *)authResult.credential).idToken
    }];
    }
    }];
    

    Using the OAuth access token, you can call the Microsoft Graph API.

    For example, to get basic profile information, you can call the REST API, passing the access token in the Authorization header:

    https://graph.microsoft.com/v1.0/me

    Unlike other providers supported by Firebase Auth, Microsoft does not provide a photo URL and instead, the binary data for a profile photo has to be requested via Microsoft Graph API.

    In addition to the OAuth access token, the user's OAuth ID token can also be retrieved from the OAuthCredential object. The sub claim in the ID token is app-specific and will not match the federated user identifier used by Firebase Auth and accessible via user.providerData[0].uid. The oid claim field should be used instead. When using a Azure AD tenant to sign-in, the oid claim will be an exact match. However for the non-tenant case, the oid field is padded. For a federated ID 4b2eabcdefghijkl, the oid will have have a form 00000000-0000-0000-4b2e-abcdefghijkl.

  7. While the above examples focus on sign-in flows, you also have the ability to link a Microsoft provider to an existing user using linkWithCredential. For example, you can link multiple providers to the same user allowing them to sign in with either.

    Swift

    Auth().currentUser.link(withCredential:credential){authResult,errorin
    iferror!=nil{
    // Handle error.
    }
    // Microsoft credential is linked to the current user.
    // IdP data available in authResult.additionalUserInfo.profile.
    // OAuth access token can also be retrieved:
    // (authResult.credential as? OAuthCredential)?.accessToken
    // OAuth ID token can also be retrieved:
    // (authResult.credential as? OAuthCredential)?.idToken
    }
    

    Objective-C

    [[FIRAuthauth].currentUser
    linkWithCredential:credential
    completion:^(FIRAuthDataResult*_NullableauthResult,NSError*_Nullableerror){
    if(error){
    // Handle error.
    }
    // Microsoft credential is linked to the current user.
    // IdP data available in authResult.additionalUserInfo.profile.
    // OAuth access token can also be retrieved:
    // ((FIROAuthCredential *)authResult.credential).accessToken
    // OAuth ID token can also be retrieved:
    // ((FIROAuthCredential *)authResult.credential).idToken
    }];
    
  8. The same pattern can be used with reauthenticateWithCredential which can be used to retrieve fresh credentials for sensitive operations that require recent login.

    Swift

    Auth().currentUser.reauthenticateWithCredential(withCredential:credential){authResult,errorin
    iferror!=nil{
    // Handle error.
    }
    // User is re-authenticated with fresh tokens minted and
    // should be able to perform sensitive operations like account
    // deletion and email or password update.
    // IdP data available in result.additionalUserInfo.profile.
    // Additional OAuth access token can also be retrieved:
    // (authResult.credential as? OAuthCredential)?.accessToken
    // OAuth ID token can also be retrieved:
    // (authResult.credential as? OAuthCredential)?.idToken
    }
    

    Objective-C

    [[FIRAuthauth].currentUser
    reauthenticateWithCredential:credential
    completion:^(FIRAuthDataResult*_NullableauthResult,NSError*_Nullableerror){
    if(error){
    // Handle error.
    }
    // User is re-authenticated with fresh tokens minted and
    // should be able to perform sensitive operations like account
    // deletion and email or password update.
    // IdP data available in result.additionalUserInfo.profile.
    // Additional OAuth access token can also be retrieved:
    // ((FIROAuthCredential *)authResult.credential).accessToken
    // OAuth ID token can also be retrieved:
    // ((FIROAuthCredential *)authResult.credential).idToken
    }];
    

Handling account-exists-with-different-credential Errors

If you enabled the One account per email address setting in the Firebase console, when a user tries to sign in a to a provider (such as Microsoft) with an email that already exists for another Firebase user's provider (such as Google), the error FIRAuthErrorCodeAccountExistsWithDifferentCredential is thrown along with a temporary FIRAuthCredential object (Microsoft credential). To complete the sign in to the intended provider, the user has to sign first to the existing provider (Google) and then link to the former FIRAuthCredential (Microsoft credential). This would look as illustrated below:

Swift

// Sign-in with an OAuth credential.
provider.getCredentialWith(nil){credential,errorin
// An account with the same email already exists.
if(errorasNSError?)?.code==AuthErrorCode.accountExistsWithDifferentCredential.rawValue{
// Get pending credential and email of existing account.
letexistingAcctEmail=(error!asNSError).userInfo[AuthErrorUserInfoEmailKey]as!String
letpendingCred=(error!asNSError).userInfo[AuthErrorUserInfoUpdatedCredentialKey]as!AuthCredential
// Lookup existing account identifier by the email.
Auth.auth().fetchProviders(forEmail:existingAcctEmail){providers,errorin
// Existing email/password account.
if(providers?.contains(EmailAuthProviderID))!{
// Existing password account for email. Ask user to provide the password of the
// existing account.
// Sign in with existing account.
Auth.auth().signIn(withEmail:existingAcctEmail,password:password){user,errorin
// Successfully signed in.
ifuser!=nil{
// Link pending credential to account.
Auth.auth().currentUser?.linkAndRetrieveData(with:pendingCred){result,errorin
// ...
}
}
}
}
}
return
}
// Other errors.
iferror!=nil{
// handle the error.
return
}
// Sign in with the credential.
ifcredential!=nil{
Auth.auth().signInAndRetrieveData(with:credential!){result,errorin
iferror!=nil{
// handle the error.
return
}
}
}
}

Objective-C

// Sign-in with an OAuth credential.
[providergetCredentialWithUIDelegate:nil
completion:^(FIRAuthCredential*_Nullablecredential,NSError*_Nullableerror){
// An account with the same email already exists.
if(error.code==FIRAuthErrorCodeAccountExistsWithDifferentCredential){
// Get pending credential and email of existing account.
NSString*existingAcctEmail=error.userInfo[FIRAuthErrorUserInfoEmailKey];
FIRAuthCredential*pendingCred=error.userInfo[FIRAuthErrorUserInfoUpdatedCredentialKey];
// Lookup existing account identifier by the email.
[[FIRAuthauth]fetchProvidersForEmail:existingAcctEmail
completion:^(NSArray<NSString*>*_Nullableproviders,
NSError*_Nullableerror){
// Existing email/password account.
if([providerscontainsObject:FIREmailAuthProviderID]){
// Existing password account for email. Ask user to provide the password of the
// existing account.
// Sign in with existing account.
[[FIRAuthauth]signInWithEmail:existingAcctEmail
password:password
completion:^(FIRUser*user,NSError*error){
// Successfully signed in.
if(user){
// Link pending credential to account.
[[FIRAuthauth].currentUserlinkWithCredential:pendingCred
completion:^(FIRUser*_Nullableuser,
NSError*_Nullableerror){
// ...
}];
}
}];
}
}];
return;
}
// Other errors.
if(error){
// handle the error.
return;
}
// Sign in with the credential.
if(credential){
[[FIRAuthauth]signInAndRetrieveDataWithCredential:credential
completion:^(FIRAuthDataResult*_NullableauthResult,
NSError*_Nullableerror){
if(error){
// handle the error.
return;
}
}];
}
}];

Advanced: Handle the sign-in flow manually

Unlike other OAuth providers supported by Firebase such as Google, Facebook, and Twitter, where sign-in can directly be achieved with OAuth access token based credentials, Firebase Auth does not support the same capability for providers such as Microsoft due to the inability of the Firebase Auth server to verify the audience of Microsoft OAuth access tokens. This is a critical security requirement and could expose applications and websites to replay attacks where a Microsoft OAuth access token obtained for one project (attacker) can be used to sign in to another project (victim). Instead, Firebase Auth offers the ability to handle the entire OAuth flow and the authorization code exchange using the OAuth client ID and secret configured in the Firebase Console. As the authorization code can only be used in conjunction with a specific client ID/secret, an authorization code obtained for one project cannot be used with another.

If these providers are required to be used in unsupported environments, a third party OAuth library and Firebase custom authentication would need to be used. The former is needed to authenticate with the provider and the latter to exchange the provider’s credential for a custom token.

After a user signs in for the first time, a new user account is created and linked to the credentials—that is, the user name and password, phone number, or auth provider information—the user signed in with. This new account is stored as part of your Firebase project, and can be used to identify a user across every app in your project, regardless of how the user signs in.

  • In your apps, you can get the user's basic profile information from the User object. See Manage Users.

  • In your Firebase Realtime Database and Cloud Storage Security Rules, you can get the signed-in user's unique user ID from the auth variable, and use it to control what data a user can access.

You can allow users to sign in to your app using multiple authentication providers by linking auth provider credentials to an existing user account.

To sign out a user, call signOut:.

Swift

letfirebaseAuth=Auth.auth()
do{
tryfirebaseAuth.signOut()
}catchletsignOutErrorasNSError{
print("Error signing out: %@",signOutError)
}

Objective-C

NSError*signOutError;
BOOLstatus=[[FIRAuthauth]signOut:&signOutError];
if(!status){
NSLog(@"Error signing out: %@",signOutError);
return;
}

You may also want to add error handling code for the full range of authentication errors. See Handle Errors.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年10月31日 UTC.