Skip to content

Commit cde2265

Browse files
committed
indent
1 parent 68169cc commit cde2265

File tree

1 file changed

+35
-39
lines changed

1 file changed

+35
-39
lines changed

articles/batch/batch-aad-auth.md

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -222,31 +222,29 @@ To authenticate with integrated authentication from Batch .NET:
222222
private const string RedirectUri = "https://<redirect-uri>";
223223
```
224224

225-
1. Write a callback method to acquire the authentication token from Azure AD. The following example calls MSAL to authenticate a user who's interacting with the application. The application proceeds once the user provides credentials, unless the app has already cached the credentials.
226-
227-
This method uses [ConfidentialClientApplicationBuilder.Create](/dotnet/api/microsoft.identity.client.confidentialclientapplicationbuilder.create) to instantiate `IConfidentialClientApplication`. The MSAL [IConfidentialClientApplication.AcquireTokenByAuthorizationCode](/dotnet/api/microsoft.identity.client.iconfidentialclientapplication.acquiretokenbyauthorizationcode) method prompts the user for their credentials.
225+
1. Write a callback method to acquire the authentication token from Azure AD. The following example calls MSAL to authenticate a user who's interacting with the application. The example uses [ConfidentialClientApplicationBuilder.Create](/dotnet/api/microsoft.identity.client.confidentialclientapplicationbuilder.create) to instantiate `IConfidentialClientApplication`. The MSAL [IConfidentialClientApplication.AcquireTokenByAuthorizationCode](/dotnet/api/microsoft.identity.client.iconfidentialclientapplication.acquiretokenbyauthorizationcode) method prompts the user for their credentials. The application proceeds once the user provides credentials.
228226

229227
`WithRedirectUri` specifies the redirect URI that the authorization server redirects the user to after authentication. The *authorizationCode* parameter is the authorization code obtained from the authorization server after the user authenticates.
230228

231229
```csharp
232-
public static async Task<string> GetTokenUsingAuthorizationCode(string authorizationCode, string redirectUri, string[] scopes)
233-
{
234-
var app = ConfidentialClientApplicationBuilder.Create(ClientId)
235-
.WithAuthority(new Uri(AuthorityUri))
236-
.WithRedirectUri(RedirectUri)
237-
.Build();
238-
239-
var authResult = await app.AcquireTokenByAuthorizationCode(scopes, authorizationCode).ExecuteAsync();
240-
return authResult.AccessToken;
241-
}
242-
```
230+
public static async Task<string> GetTokenUsingAuthorizationCode(string authorizationCode, string redirectUri, string[] scopes)
231+
{
232+
var app = ConfidentialClientApplicationBuilder.Create(ClientId)
233+
.WithAuthority(AuthorityUri)
234+
.WithRedirectUri(RedirectUri)
235+
.Build();
236+
237+
var authResult = await app.AcquireTokenByAuthorizationCode(scopes, authorizationCode).ExecuteAsync();
238+
return authResult.AccessToken;
239+
}
240+
```
243241

244242
1. Call this method with the following code, replacing `<authorization-code>` with the authorization code obtained from the authorization server. The `.default` scope ensures that the user has permission to access all the scopes for the resource.
245243

246-
```csharp
247-
248-
var token = await GetTokenUsingAuthorizationCode("<authorization-code>", "RedirectUri", new string[] { "BatchResourceUri/.default" });
249-
```
244+
```csharp
245+
246+
var token = await GetTokenUsingAuthorizationCode("<authorization-code>", "RedirectUri", new string[] { "BatchResourceUri/.default" });
247+
```
250248

251249
1. Construct a **BatchTokenCredentials** object that takes the delegate as a parameter. Use those credentials to open a **BatchClient** object. Then use the **BatchClient** object for subsequent operations against the Batch service:
252250

@@ -308,23 +306,23 @@ To authenticate with a service principal from Batch .NET:
308306

309307
1. Write a callback method to acquire the authentication token from Azure AD. The following [ConfidentialClientApplicationBuilder.Create](/dotnet/api/microsoft.identity.client.confidentialclientapplicationbuilder.create) method calls MSAL for unattended authentication.
310308

311-
```csharp
312-
public static async Task<string> GetAccessToken(string[] scopes)
313-
{
314-
var app = ConfidentialClientApplicationBuilder.Create(clientId)
315-
.WithClientSecret(ClientKey)
316-
.WithAuthority(new Uri(AuthorityUri))
317-
.Build();
318-
319-
var result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
320-
return result.AccessToken;
321-
}
322-
```
309+
```csharp
310+
public static async Task<string> GetAccessToken(string[] scopes)
311+
{
312+
var app = ConfidentialClientApplicationBuilder.Create(clientId)
313+
.WithClientSecret(ClientKey)
314+
.WithAuthority(new Uri(AuthorityUri))
315+
.Build();
316+
317+
var result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
318+
return result.AccessToken;
319+
}
320+
```
323321
1. Call this method by using the following code. The `.default` scope ensures that the application has permission to access all the scopes for the resource.
324322

325-
```csharp
326-
var token = await GetAccessToken(new string[] { "BatchResourceId/.default" });
327-
```
323+
```csharp
324+
var token = await GetAccessToken(new string[] { "BatchResourceId/.default" });
325+
```
328326

329327
1. Construct a **BatchTokenCredentials** object that takes the delegate as a parameter. Use those credentials to open a **BatchClient** object. Then use the **BatchClient** object for subsequent operations against the Batch service:
330328

@@ -344,14 +342,16 @@ public static async Task<string> GetAccessToken(string[] scopes)
344342

345343
To authenticate with a service principal from Batch Python:
346344

347-
1. Install the [azure-batch](https://pypi.org/project/azure-batch/) and [azure-common](https://pypi.org/project/azure-common/) Python modules. Reference the modules:
345+
1. Install the [azure-batch](https://pypi.org/project/azure-batch/) and [azure-common](https://pypi.org/project/azure-common/) Python modules.
346+
347+
1. Reference the modules:
348348

349349
```python
350350
from azure.batch import BatchServiceClient
351351
from azure.common.credentials import ServicePrincipalCredentials
352352
```
353353

354-
1. When you use a service principal, you must provide a tenant-specific endpoint. You can get your tenant ID from the Azure AD **Overview** page or **Properties** page in the Azure portal.
354+
1. To use a service principal, provide a tenant-specific endpoint. You can get your tenant ID from the Azure AD **Overview** page or **Properties** page in the Azure portal.
355355

356356
```python
357357
TENANT_ID = "<tenant-id>"
@@ -405,10 +405,6 @@ For a Python example of how to create a Batch client authenticated by using an A
405405

406406
## Next steps
407407

408-
- For in-depth examples that show how to use MSAL, see the [Azure code samples library](/samples/browse/?products=microsoft-authentication-library).
409-
410-
For more information, see:
411-
412408
- [Authenticate Batch Management solutions with Active Directory](batch-aad-auth-management.md)
413409
- [Client credential flows in MSAL.NET](/entra/msal/dotnet/acquiring-tokens/web-apps-apis/client-credential-flows)
414410
- [Using MSAL.NET to get tokens by authorization code (for web sites)](/entra/msal/dotnet/acquiring-tokens/web-apps-apis/authorization-codes)

0 commit comments

Comments
 (0)