|
| 1 | +--- |
| 2 | +title: Configure MSAL for iOS and macOS to use different identity providers | Microsoft identity platform |
| 3 | +description: Learn how to use different authorities such as B2C, sovereign clouds, and guest users, with MSAL for iOS and macOS. |
| 4 | +services: active-directory |
| 5 | +documentationcenter: '' |
| 6 | +author: tylermsft |
| 7 | +manager: CelesteDG |
| 8 | +editor: '' |
| 9 | + |
| 10 | +ms.service: active-directory |
| 11 | +ms.subservice: develop |
| 12 | +ms.workload: identity |
| 13 | +ms.tgt_pltfrm: na |
| 14 | +ms.devlang: na |
| 15 | +ms.topic: conceptual |
| 16 | +ms.date: 08/28/2019 |
| 17 | +ms.author: twhitney |
| 18 | +ms.reviewer: '' |
| 19 | +ms.custom: aaddev |
| 20 | +ms.collection: M365-identity-device-management |
| 21 | +--- |
| 22 | + |
| 23 | +# How to: Configure MSAL for iOS and macOS to use different identity providers |
| 24 | + |
| 25 | +This article will show you how to configure your Microsoft authentication library app for iOS and macOS (MSAL) for different authorities such as Azure Active Directory (Azure AD), Business-to-Consumer (B2C), sovereign clouds, and guest users. Throughout this article, you can generally think of an authority as an identity provider. |
| 26 | + |
| 27 | +## Default authority configuration |
| 28 | + |
| 29 | +`MSALPublicClientApplication` is configured with a default authority URL of `https://login.microsoftonline.com/common`, which is suitable for most Azure Active Directory (AAD) scenarios. Unless you're implementing advanced scenarios like national clouds, or working with B2C, you won't need to change it. |
| 30 | + |
| 31 | +> [!NOTE] |
| 32 | +> Modern authentication with Active Directory Federation Services as identity provider (ADFS) is not supported (see [ADFS for Developers](https://docs.microsoft.com/windows-server/identity/ad-fs/overview/ad-fs-scenarios-for-developers) for details). ADFS is supported through federation. |
| 33 | +
|
| 34 | +## Change the default authority |
| 35 | + |
| 36 | +In some scenarios, such as business-to-consumer (B2C), you may need to change the default authority. |
| 37 | + |
| 38 | +### B2C |
| 39 | + |
| 40 | +To work with B2C, the [Microsoft Authentication Library (MSAL)](reference-v2-libraries.md) requires a different authority configuration. MSAL recognizes one authority URL format as B2C by itself. The recognized B2C authority format is `https://<host>/tfp/<tenant>/<policy>`, for example `https://login.microsoftonline.com/tfp/contoso.onmicrosoft.com/B2C_1_SignInPolicy`. However, you can also use any other supported B2C authority URLs by declaring authority as B2C authority explicitly. |
| 41 | + |
| 42 | +To support an arbitrary URL format for B2C, `MSALB2CAuthority` can be set with an arbitrary URL, like this: |
| 43 | + |
| 44 | +Objective-C |
| 45 | +```objc |
| 46 | +NSURL *authorityURL = [NSURL URLWithString:@"arbitrary URL"]; |
| 47 | +MSALB2CAuthority *b2cAuthority = [[MSALB2CAuthority alloc] initWithURL:authorityURL |
| 48 | + error:&b2cAuthorityError]; |
| 49 | +``` |
| 50 | +Swift |
| 51 | +```swift |
| 52 | +guard let authorityURL = URL(string: "arbitrary URL") else { |
| 53 | + // Handle error |
| 54 | + return |
| 55 | +} |
| 56 | +let b2cAuthority = try MSALB2CAuthority(url: authorityURL) |
| 57 | +``` |
| 58 | + |
| 59 | +All B2C authorities that don't use the default B2C authority format must be declared as known authorities. |
| 60 | + |
| 61 | +Add each different B2C authority to the known authorities list even if authorities only differ in policy. |
| 62 | + |
| 63 | +Objective-C |
| 64 | +```objc |
| 65 | +MSALPublicClientApplicationConfig *b2cApplicationConfig = [[MSALPublicClientApplicationConfig alloc] |
| 66 | + initWithClientId:@"your-client-id" |
| 67 | + redirectUri:@"your-redirect-uri" |
| 68 | + authority:b2cAuthority]; |
| 69 | +b2cApplicationConfig.knownAuthorities = @[b2cAuthority]; |
| 70 | +``` |
| 71 | +Swift |
| 72 | +```swift |
| 73 | +let b2cApplicationConfig = MSALPublicClientApplicationConfig(clientId: "your-client-id", redirectUri: "your-redirect-uri", authority: b2cAuthority) |
| 74 | +b2cApplicationConfig.knownAuthorities = [b2cAuthority] |
| 75 | +``` |
| 76 | + |
| 77 | +When your app requests a new policy, the authority URL needs to be changed because the authority URL is different for each policy. |
| 78 | + |
| 79 | +To configure a B2C application, set `@property MSALAuthority *authority` with an instance of `MSALB2CAuthority` in `MSALPublicClientApplicationConfig` before creating `MSALPublicClientApplication`, like this: |
| 80 | + |
| 81 | +Objective-C |
| 82 | +```ObjC |
| 83 | + // Create B2C authority URL |
| 84 | + NSURL *authorityURL = [NSURL URLWithString:@"https://login.microsoftonline.com/tfp/contoso.onmicrosoft.com/B2C_1_SignInPolicy"]; |
| 85 | + |
| 86 | + MSALB2CAuthority *b2cAuthority = [[MSALB2CAuthority alloc] initWithURL:authorityURL |
| 87 | + error:&b2cAuthorityError]; |
| 88 | + if (!b2cAuthority) |
| 89 | + { |
| 90 | + // Handle error |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + // Create MSALPublicClientApplication configuration |
| 95 | + MSALPublicClientApplicationConfig *b2cApplicationConfig = [[MSALPublicClientApplicationConfig alloc] |
| 96 | + initWithClientId:@"your-client-id" |
| 97 | + redirectUri:@"your-redirect-uri" |
| 98 | + authority:b2cAuthority]; |
| 99 | + |
| 100 | + // Initialize MSALPublicClientApplication |
| 101 | + MSALPublicClientApplication *b2cApplication = |
| 102 | + [[MSALPublicClientApplication alloc] initWithConfiguration:b2cApplicationConfig error:&error]; |
| 103 | + |
| 104 | + if (!b2cApplication) |
| 105 | + { |
| 106 | + // Handle error |
| 107 | + return; |
| 108 | + } |
| 109 | +``` |
| 110 | +Swift |
| 111 | +```swift |
| 112 | +do{ |
| 113 | + // Create B2C authority URL |
| 114 | + guard let authorityURL = URL(string: "https://login.microsoftonline.com/tfp/contoso.onmicrosoft.com/B2C_1_SignInPolicy") else { |
| 115 | + // Handle error |
| 116 | + return |
| 117 | + } |
| 118 | + let b2cAuthority = try MSALB2CAuthority(url: authorityURL) |
| 119 | +
|
| 120 | + // Create MSALPublicClientApplication configuration |
| 121 | + let b2cApplicationConfig = MSALPublicClientApplicationConfig(clientId: "your-client-id", redirectUri: "your-redirect-uri", authority: b2cAuthority) |
| 122 | +
|
| 123 | + // Initialize MSALPublicClientApplication |
| 124 | + let b2cApplication = try MSALPublicClientApplication(configuration: b2cApplicationConfig) |
| 125 | +} catch { |
| 126 | + // Handle error |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +### Sovereign clouds |
| 131 | + |
| 132 | +If your app runs in a sovereign cloud, you may need to change the authority URL in the `MSALPublicClientApplication`. The following example sets the authority URL to work with the German AAD cloud: |
| 133 | + |
| 134 | +Objective-C |
| 135 | +```objc |
| 136 | + NSURL *authorityURL = [NSURL URLWithString:@"https://login.microsoftonline.de/common"]; |
| 137 | + MSALAuthority *sovereignAuthority = [MSALAuthority authorityWithURL:authorityURL error:&authorityError]; |
| 138 | + |
| 139 | + if (!sovereignAuthority) |
| 140 | + { |
| 141 | + // Handle error |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + MSALPublicClientApplicationConfig *applicationConfig = [[MSALPublicClientApplicationConfig alloc] |
| 146 | + initWithClientId:@"your-client-id" |
| 147 | + redirectUri:@"your-redirect-uri" |
| 148 | + authority:sovereignAuthority]; |
| 149 | + |
| 150 | + |
| 151 | + MSALPublicClientApplication *sovereignApplication = [[MSALPublicClientApplication alloc] initWithConfiguration:applicationConfig error:&error]; |
| 152 | + |
| 153 | + |
| 154 | + if (!sovereignApplication) |
| 155 | + { |
| 156 | + // Handle error |
| 157 | + return; |
| 158 | + } |
| 159 | +``` |
| 160 | +Swift |
| 161 | +```swift |
| 162 | +do{ |
| 163 | + guard let authorityURL = URL(string: "https://login.microsoftonline.de/common") else { |
| 164 | + //Handle error |
| 165 | + return |
| 166 | + } |
| 167 | + let sovereignAuthority = try MSALAuthority(url: authorityURL) |
| 168 | + |
| 169 | + let applicationConfig = MSALPublicClientApplicationConfig(clientId: "your-client-id", redirectUri: "your-redirect-uri", authority: sovereignAuthority) |
| 170 | + |
| 171 | + let sovereignApplication = try MSALPublicClientApplication(configuration: applicationConfig) |
| 172 | +} catch { |
| 173 | + // Handle error |
| 174 | +} |
| 175 | +``` |
| 176 | + |
| 177 | +You may need to pass different scopes to each sovereign cloud. Which scopes to send depends on the resource that you're using. For example, you might use `"https://graph.microsoft.com/user.read"` in worldwide cloud, and `"https://graph.microsoft.de/user.read"` in German cloud. |
| 178 | + |
| 179 | +### Signing a user into a specific tenant |
| 180 | + |
| 181 | +When the authority URL is set to `"login.microsoftonline.com/common"`, the user will be signed into their home tenant. However, some apps may need to sign the user into a different tenant and some apps only work with a single tenant. |
| 182 | + |
| 183 | +To sign the user into a specific tenant, configure `MSALPublicClientApplication` with a specific authority. For example: |
| 184 | + |
| 185 | +`https://login.microsoftonline.com/469fdeb4-d4fd-4fde-991e-308a78e4bea4` |
| 186 | + |
| 187 | +The following shows how to sign a user into a specific tenant: |
| 188 | + |
| 189 | +Objective-C |
| 190 | +```objc |
| 191 | + NSURL *authorityURL = [NSURL URLWithString:@"https://login.microsoftonline.com/469fdeb4-d4fd-4fde-991e-308a78e4bea4"]; |
| 192 | + MSALAADAuthority *tenantedAuthority = [[MSALAADAuthority alloc] initWithURL:authorityURL error:&authorityError]; |
| 193 | + |
| 194 | + if (!tenantedAuthority) |
| 195 | + { |
| 196 | + // Handle error |
| 197 | + return; |
| 198 | + } |
| 199 | + |
| 200 | + MSALPublicClientApplicationConfig *applicationConfig = [[MSALPublicClientApplicationConfig alloc] |
| 201 | + initWithClientId:@"your-client-id" |
| 202 | + redirectUri:@"your-redirect-uri" |
| 203 | + authority:tenantedAuthority]; |
| 204 | + |
| 205 | + MSALPublicClientApplication *application = |
| 206 | + [[MSALPublicClientApplication alloc] initWithConfiguration:applicationConfig error:&error]; |
| 207 | + |
| 208 | + if (!application) |
| 209 | + { |
| 210 | + // Handle error |
| 211 | + return; |
| 212 | + } |
| 213 | +``` |
| 214 | +Swift |
| 215 | +```swift |
| 216 | +do{ |
| 217 | + guard let authorityURL = URL(string: "https://login.microsoftonline.com/469fdeb4-d4fd-4fde-991e-308a78e4bea4") else { |
| 218 | + //Handle error |
| 219 | + return |
| 220 | + } |
| 221 | + let tenantedAuthority = try MSALAADAuthority(url: authorityURL) |
| 222 | + |
| 223 | + let applicationConfig = MSALPublicClientApplicationConfig(clientId: "your-client-id", redirectUri: "your-redirect-uri", authority: tenantedAuthority) |
| 224 | + |
| 225 | + let application = try MSALPublicClientApplication(configuration: applicationConfig) |
| 226 | +} catch { |
| 227 | + // Handle error |
| 228 | +} |
| 229 | +``` |
| 230 | + |
| 231 | +## Supported authorities |
| 232 | + |
| 233 | +### MSALAuthority |
| 234 | + |
| 235 | +The `MSALAuthority` class is the base abstract class for the MSAL authority classes. Don't try to create instance of it using `alloc` or `new`. Instead, either create one of its subclasses directly (`MSALAADAuthority`, `MSALB2CAuthority`) or use the factory method `authorityWithURL:error:` to create subclasses using an authority URL. |
| 236 | + |
| 237 | +Use the `url` property to get a normalized authority URL. Extra parameters and path components or fragments that aren't part of authority won't be in the returned normalized authority URL. |
| 238 | + |
| 239 | +The following are subclasses of `MSALAuthority` that you can instantiate depending on the authority want to use. |
| 240 | + |
| 241 | +### MSALAADAuthority |
| 242 | + |
| 243 | +`MSALAADAuthority` represents an AAD authority. The authority url should be in the following format, where `<port>` is optional: `https://<host>:<port>/<tenant>` |
| 244 | + |
| 245 | +### MSALB2CAuthority |
| 246 | + |
| 247 | +`MSALB2CAuthority` represents a B2C authority. By default, the B2C authority url should be in the following format, where `<port>` is optional: `https://<host>:<port>/tfp/<tenant>/<policy>`. However, MSAL also supports other arbitrary B2C authority formats. |
| 248 | + |
| 249 | +## Next steps |
| 250 | + |
| 251 | +Learn more about [Authentication flows and application scenarios](authentication-flows-app-scenarios.md) |
0 commit comments