Cline rules
.clinerules/03-MSAL-API-usage.mdCline rules
Quality
58/100
Scores the file, not the repository.Length
718 words
9 headings · 4 code blocksRepository
344
— · pushed 0 days agoLast changed
3 days ago
First indexed 3 days ago.1# MSAL API Usage Examples23This document provides code snippets for common MSAL authentication patterns in both Swift and Objective-C.45## Interactive Token Acquisition67Interactive token acquisition presents a UI to the user for authentication. This is typically used for initial sign-in or when a silent token acquisition fails.89### Swift1011```swift12import MSAL1314// Configure the application15let config = MSALPublicClientApplicationConfig(clientId: "YOUR_CLIENT_ID")16let application = try MSALPublicClientApplication(configuration: config)1718// Configure webview parameters19let webViewParameters = MSALWebviewParameters(authPresentationViewController: self)2021// Create interactive token parameters22let interactiveParameters = MSALInteractiveTokenParameters(scopes: ["user.read"],23 webviewParameters: webViewParameters)2425// Acquire token interactively26application.acquireToken(with: interactiveParameters) { (result, error) in27 guard let result = result else {28 print("Could not acquire token: \(error?.localizedDescription ?? "Unknown error")")29 return30 }3132 let accessToken = result.accessToken33 let account = result.account34 print("Access token acquired: \(accessToken)")35 print("Account: \(account.username ?? "Unknown")")36}37```3839### Objective-C4041```objc42#import <MSAL/MSAL.h>4344// Configure the application45MSALPublicClientApplicationConfig *config =46 [[MSALPublicClientApplicationConfig alloc] initWithClientId:@"YOUR_CLIENT_ID"];4748NSError *error = nil;49MSALPublicClientApplication *application =50 [[MSALPublicClientApplication alloc] initWithConfiguration:config error:&error];5152if (error) {53 NSLog(@"Failed to create application: %@", error);54 return;55}5657// Configure webview parameters58MSALWebviewParameters *webViewParameters =59 [[MSALWebviewParameters alloc] initWithAuthPresentationViewController:self];6061// Create interactive token parameters62MSALInteractiveTokenParameters *interactiveParams =63 [[MSALInteractiveTokenParameters alloc] initWithScopes:@[@"user.read"]64 webviewParameters:webViewParameters];6566// Acquire token interactively67[application acquireTokenWithParameters:interactiveParams68 completionBlock:^(MSALResult * _Nullable result, NSError * _Nullable error) {69 if (error) {70 NSLog(@"Could not acquire token: %@", error);71 return;72 }7374 NSString *accessToken = result.accessToken;75 MSALAccount *account = result.account;76 NSLog(@"Access token acquired: %@", accessToken);77 NSLog(@"Account: %@", account.username);78}];79```8081## Silent Token Acquisition8283Silent token acquisition attempts to get a token without user interaction, using cached tokens or refresh tokens. This is the recommended approach for acquiring tokens in most scenarios.8485### Swift8687```swift88import MSAL8990// Configure the application91let config = MSALPublicClientApplicationConfig(clientId: "YOUR_CLIENT_ID")92let application = try MSALPublicClientApplication(configuration: config)9394// Get the account (from previous interactive sign-in)95guard let account = try application.accountForIdentifier("ACCOUNT_IDENTIFIER") else {96 print("Account not found")97 return98}99100// Create silent token parameters101let silentParameters = MSALSilentTokenParameters(scopes: ["user.read"],102 account: account)103104// Acquire token silently105application.acquireTokenSilent(with: silentParameters) { (result, error) in106 if let error = error as NSError? {107 // Check if interaction is required108 if error.domain == MSALErrorDomain &&109 error.code == MSALError.interactionRequired.rawValue {110 // Fall back to interactive token acquisition111 print("Interaction required, use interactive flow")112 } else {113 print("Could not acquire token silently: \(error.localizedDescription)")114 }115 return116 }117118 guard let result = result else {119 print("No result returned")120 return121 }122123 let accessToken = result.accessToken124 print("Access token acquired silently: \(accessToken)")125}126```127128### Objective-C129130```objc131#import <MSAL/MSAL.h>132133// Configure the application134MSALPublicClientApplicationConfig *config =135 [[MSALPublicClientApplicationConfig alloc] initWithClientId:@"YOUR_CLIENT_ID"];136137NSError *error = nil;138MSALPublicClientApplication *application =139 [[MSALPublicClientApplication alloc] initWithConfiguration:config error:&error];140141if (error) {142 NSLog(@"Failed to create application: %@", error);143 return;144}145146// Get the account (from previous interactive sign-in)147MSALAccount *account = [application accountForIdentifier:@"ACCOUNT_IDENTIFIER" error:&error];148149if (!account) {150 NSLog(@"Account not found");151 return;152}153154// Create silent token parameters155MSALSilentTokenParameters *silentParams =156 [[MSALSilentTokenParameters alloc] initWithScopes:@[@"user.read"]157 account:account];158159// Acquire token silently160[application acquireTokenSilentWithParameters:silentParams161 completionBlock:^(MSALResult * _Nullable result, NSError * _Nullable error) {162 if (error) {163 // Check if interaction is required164 if ([error.domain isEqualToString:MSALErrorDomain] &&165 error.code == MSALErrorInteractionRequired) {166 // Fall back to interactive token acquisition167 NSLog(@"Interaction required, use interactive flow");168 } else {169 NSLog(@"Could not acquire token silently: %@", error);170 }171 return;172 }173174 NSString *accessToken = result.accessToken;175 NSLog(@"Access token acquired silently: %@", accessToken);176}];177```178179## Best Practices1801811. **Always try silent acquisition first**: Before prompting the user, attempt to acquire a token silently using `acquireTokenSilentWithParameters:completionBlock:`.1821832. **Handle interaction required errors**: When silent acquisition fails with `MSALErrorInteractionRequired`, fall back to interactive acquisition using `acquireTokenWithParameters:completionBlock:`.1841853. **Cache the account identifier**: Store the account identifier (`account.identifier`) after successful interactive sign-in for use in subsequent silent token requests.1861874. **Use appropriate scopes**: Request only the scopes your application needs. The Microsoft Graph API uses scopes like `user.read`, `mail.read`, etc.1881895. **Configure the application once**: Create a single instance of `MSALPublicClientApplication` and reuse it throughout your app's lifecycle.1901916. **Handle errors gracefully**: Implement proper error handling for network issues, user cancellations, and authentication failures.1921937. **Use MSALPublicClientApplicationConfig**: Always initialize MSAL using `MSALPublicClientApplicationConfig` to take advantage of all configuration options.194195## Additional Resources196197- For more information about MSAL scopes, see the Microsoft Graph permissions reference198- For authority configuration, see the Azure AD documentation on authentication endpoints199- For broker integration on iOS, ensure your redirect URI is properly configured in the Azure portal200
Also in AzureAD/microsoft-authentication-library-for-objc
Diff this repo’s formatsOne repository carrying more than one format is the comparison this product exists for: does anyone actually write different content in each file, or is one a copy of the other?
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| AzureAD/microsoft-authentication-library-for-objc.clinerules/02-External-tenant-configuration.md · 344 | Cline rules | setupbuildteststyle+7 | 69/100 | 3 days ago | |
| AzureAD/microsoft-authentication-library-for-objcCLAUDE.md · 344 | CLAUDE.md | buildteststylearch+6 | 76/100 | 3 days ago | |
| AzureAD/microsoft-authentication-library-for-objc.clinerules/01-Workforce-tenant-configuration.md · 344 | Cline rules | setupbuildtestarch+5 | 58/100 | 3 days ago | |
| AzureAD/microsoft-authentication-library-for-objc.clinerules/04-Code-style-guidelines.md · 344 | Cline rules | stylearchtypestesting-strategy+4 | 69/100 | 3 days ago | |
| AzureAD/microsoft-authentication-library-for-objc.clinerules/05-feature-gating.md · 344 | Cline rules | stylearchdependenciesperformance+2 | 61/100 | 3 days ago | |
| AzureAD/microsoft-authentication-library-for-objc.clinerules/06-Customer-communication-guidelines.md · 344 | Cline rules | stylemonorepodo-notagent-behaviour | 51/100 | 3 days ago | |
| AzureAD/microsoft-authentication-library-for-objc.clinerules/AGENTS.md · 344 | AGENTS.md | styleapi | 48/100 | 3 days ago | |
| AzureAD/microsoft-authentication-library-for-objc.cursor/rules/ruler_cursor_instructions.mdc · 344 | Cursor rules | buildteststylearch+6 | 76/100 | 3 days ago | |
| AzureAD/microsoft-authentication-library-for-objc.github/copilot-instructions.md · 344 | Copilot instructions | setupbuildteststyle+11 | 64/100 | 3 days ago | |
| AzureAD/microsoft-authentication-library-for-objcAGENTS.md · 344 | AGENTS.md | buildteststylearch+6 | 76/100 | 3 days ago |
Diff against .clinerules/02-External-tenant-configuration.md Diff against CLAUDE.md Diff against .clinerules/01-Workforce-tenant-configuration.md Diff against .clinerules/04-Code-style-guidelines.md Diff against .clinerules/05-feature-gating.md Diff against .clinerules/06-Customer-communication-guidelines.md Diff against .clinerules/AGENTS.md Diff against .cursor/rules/ruler_cursor_instructions.mdc Diff against .github/copilot-instructions.md Diff against AGENTS.md
Similar configs
Same format, overlapping stack, ranked by quality.
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| JCodesMore/ai-website-cloner-template.clinerules · 31k | Cline rules | buildlint-formatstylearch+3 | 97/100 | 2 days ago | |
| BryaanF/LiantPortfolio.clinerules/project-guidelines.md · 0 | Cline rules | buildstylearchgit+2 | 96/100 | 3 days ago | |
| lepinkainen/humanlog.clinerules/project-rules.md · 0 | Cline rules | setupbuildtestlint-format+8 | 96/100 | 3 days ago | |
| u9401066/zotero-keeper.clinerules/50-pubmed-project.md · 6 | Cline rules | testlint-formatstylearch+1 | 94/100 | 3 days ago | |
| u9401066/zotero-keepervscode-extension/resources/repo-assets/pubmed-search-mcp/.clinerules/50-pubmed-project.md · 6 | Cline rules | testlint-formatstylearch+1 | 94/100 | 3 days ago | |
| u9401066/pubmed-search-mcp.clinerules/50-pubmed-project.md · 23 | Cline rules | testlint-formatstylearch+1 | 94/100 | 3 days ago | |
| VaillerTeeter/HoshimiNest.clinerules/project-identity.md · 1 | Cline rules | setuparchtypesdo-not | 93/100 | yesterday | |
| blendsdk/codeops-mcp.clinerules/project.md · 0 | Cline rules | buildteststylearch+7 | 91/100 | 3 days ago |
