title | description | services | author | manager | ms.service | ms.workload | ms.topic | ms.date | ms.author | ms.subservice | ms.custom |
---|---|---|---|---|---|---|---|---|---|---|---|
Configure authentication in a sample iOS Swift application by using Azure Active Directory B2C |
This article discusses how to use Azure Active Directory B2C to sign in and sign up users in an iOS Swift application. |
active-directory-b2c |
msmimart |
celestedg |
active-directory |
identity |
reference |
07/29/2021 |
mimart |
B2C |
b2c-support |
This article uses a sample iOS Swift application to illustrate how to add Azure Active Directory B2C (Azure AD B2C) authentication to your mobile apps.
OpenID Connect (OIDC) is an authentication protocol that's built on OAuth 2.0. You can use OIDC to securely sign users in to an application. This mobile app sample uses the Microsoft Authentication Library (MSAL) with OIDC authorization code Proof Key for Code Exchange (PKCE) flow. The MSAL is a Microsoft-provided library that simplifies adding authentication and authorization support to mobile apps.
The sign-in flow involves the following steps:
- Users open the app and select sign-in.
- The app opens the mobile device's system browser and starts an authentication request to Azure AD B2C.
- Users sign up or sign in, reset the password, or sign in with a social account.
- After users sign in successfully, Azure AD B2C returns an authorization code to the app.
- The app takes the following actions:
- It exchanges the authorization code to an ID token, access token, and refresh token.
- It reads the ID token claims.
- It stores the tokens in an in-memory cache for later use.
To enable your app to sign in with Azure AD B2C and call a web API, register two applications in the Azure AD B2C directory:
-
The mobile application registration enables your app to sign in with Azure AD B2C. During app registration, specify the redirect URI. The redirect URI is the endpoint to which users are redirected by Azure AD B2C after they've authenticated with Azure AD B2C. The app registration process generates an application ID, also known as the client ID, which uniquely identifies your mobile app (for example, App ID: 1).
-
The web API registration enables your app to call a protected web API. The registration exposes the web API permissions (scopes). The app registration process generates an application ID, which uniquely identifies your web API (for example, App ID: 2). Grant your mobile app (App ID: 1) permissions to the web API scopes (App ID: 2).
The application registration and architecture are illustrated in the following diagrams:
[!INCLUDE active-directory-b2c-app-integration-call-api]
[!INCLUDE active-directory-b2c-app-integration-sign-out-flow]
A computer that's running:
- Xcode 13 or later.
- CocoaPods dependency manager for Swift and Objective-C Cocoa projects.
[!INCLUDE active-directory-b2c-app-integration-add-user-flow]
Create the mobile app and the web API application registration, and specify the scopes of your web API.
[!INCLUDE active-directory-b2c-app-integration-register-api]
[!INCLUDE active-directory-b2c-app-integration-api-scopes]
To create the mobile app registration, do the following:
- Sign in to the Azure portal.
- Select App registrations, and then select New registration.
- Under Name, enter a name for the application (for example, iOs-app1).
- Under Supported account types, select Accounts in any identity provider or organizational directory (for authenticating users with user flows).
- Under Redirect URI, select Public client/native (mobile & desktop) and then, in the URL box, enter
msauth.com.microsoft.identitysample.MSALiOS://auth
. - Select Register.
- After the app registration is completed, select Overview.
- Record the Application (client) ID for later use, when you configure the mobile application.
[!INCLUDE active-directory-b2c-app-integration-grant-permissions]
This sample acquires an access token with the relevant scopes that the mobile app can use for a web API. To call a web API from code, do the following:
- Use an existing web API, or create a new one. For more information, see Enable authentication in your own web API by using Azure AD B2C.
- Change the sample code to call a web API.
- After you configure the web API, copy the URI of the web API endpoint. You will use the web API endpoint in the next steps.
Tip
If you don't have a web API, you can still run this sample. In this case, the app returns the access token but won't be able to call the web API.
-
Download the .zip file, or clone the sample web app from the GitHub repo.
git clone https://github.com/Azure-Samples/active-directory-b2c-ios-swift-native-msal/tree/vNext.git
-
Use CocoaPods to install the MSAL library. In a terminal window, go to the project root folder. This folder contains the podfile file. Run the following command:
pod install
-
Open the
MSALiOS.xcworkspace
workspace with Xcode.
Open the ViewController.swift file. The ViewController
class members contain information about your Azure AD B2C identity provider. The mobile app uses this information to establish a trust relationship with Azure AD B2C, sign users in and out, acquire tokens, and validate them.
Update the following class members:
Key | Value |
---|---|
kTenantName | Your Azure AD B2C tenant full tenant name (for example, contoso.onmicrosoft.com ). |
kAuthorityHostName | The first part of your Azure AD B2C tenant name (for example, contoso.b2clogin.com ). |
kClientID | The mobile application ID from step 2.3. |
kRedirectUri | The mobile application redirect URI from step 2.3, msauth.com.microsoft.identitysample.MSALiOS://auth . |
kSignupOrSigninPolicy | The sign-up or sign-in user flow or custom policy you created in step 1. |
kEditProfilePolicy | The edit profile user flow or custom policy you created in step 1. |
kGraphURI | (Optional) The web API endpoint that you created in step 3 (for example, https://contoso.azurewebsites.net/hello ). |
kScopes | The web API scopes that you created in step 2.4. |
-
Build and run the project with a simulator of a connected iOS device.
-
Select Sign In, and then sign up or sign in with your Azure AD B2C local or social account.
-
After successful authentication, you'll see your display name in the navigation bar.
Learn how to: