You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/batch/batch-aad-auth.md
+54-65Lines changed: 54 additions & 65 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
title: Authenticate Azure Batch services with Azure Active Directory
3
3
description: Learn how to authenticate Azure Batch service applications with Azure AD by using integrated authentication or a service principal.
4
4
ms.topic: how-to
5
-
ms.date: 03/30/2023
5
+
ms.date: 04/03/2023
6
6
ms.custom: has-adal-ref, subject-rbac-steps
7
7
---
8
8
@@ -182,7 +182,7 @@ The code examples in this section show how to authenticate with Azure AD by usin
182
182
183
183
To authenticate with integrated authentication from Batch .NET:
184
184
185
-
1. Install the [Azure Batch .NET](https://www.nuget.org/packages/Microsoft.Azure.Batch/)package and the [MSAL](https://www.nuget.org/packages/Microsoft.Identity.Client/) NuGet packages.
185
+
1. Install the [Azure Batch .NET](https://www.nuget.org/packages/Microsoft.Azure.Batch/) and the [MSAL](https://www.nuget.org/packages/Microsoft.Identity.Client/) NuGet packages.
186
186
187
187
1. Include the following `using` statements in your code:
188
188
@@ -222,43 +222,38 @@ To authenticate with integrated authentication from Batch .NET:
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 to authenticate a user who's interacting with the application. 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 the credentials, unless the app has already cached the 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 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.
228
+
229
+
`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.
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.
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:
using (varclient=BatchClient.Open(newBatchTokenCredentials(BatchAccountUrl, tokenProvider)))
264
259
{
@@ -273,7 +268,7 @@ To authenticate with a service principal from Batch .NET:
273
268
274
269
1. Install the [Azure Batch .NET](https://www.nuget.org/packages/Azure.Batch/) and the [MSAL](https://www.nuget.org/packages/Microsoft.Identity.Client/) NuGet packages.
275
270
276
-
1.Include the following `using` statements in your code:
271
+
1.Declare the following `using` statements in your code:
277
272
278
273
```csharp
279
274
usingMicrosoft.Azure.Batch;
@@ -311,42 +306,32 @@ To authenticate with a service principal from Batch .NET:
311
306
privateconststringClientKey="<secret-key>";
312
307
```
313
308
314
-
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:
309
+
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.
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.
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:
using (varclient=BatchClient.Open(newBatchTokenCredentials(BatchAccountUrl, tokenProvider)))
352
337
{
@@ -359,14 +344,14 @@ To authenticate with a service principal from Batch .NET:
359
344
360
345
To authenticate with a service principal from Batch Python:
361
346
362
-
1. Install and reference the [azure-batch](https://pypi.org/project/azure-batch/) and [azure-common](https://pypi.org/project/azure-common/) Python modules.
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:
363
348
364
349
```python
365
350
from azure.batch import BatchServiceClient
366
351
from azure.common.credentials import ServicePrincipalCredentials
367
352
```
368
353
369
-
1. When you use a service principal, you must provide a tenant-specific endpoint. You can get your tenant ID from the **Properties** page of your Azure AD in the Azure portal.
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.
370
355
371
356
```python
372
357
TENANT_ID="<tenant-id>"
@@ -416,13 +401,17 @@ To authenticate with a service principal from Batch Python:
416
401
)
417
402
```
418
403
404
+
For a Python example of how to create a Batch client authenticated by using an Azure AD token, see the [Deploying Azure Batch Custom Image with a Python Script sample](https://github.com/azurebigcompute/recipes/blob/master/Azure%20Batch/CustomImages/CustomImagePython.md).
405
+
419
406
## Next steps
420
407
421
408
- For in-depth examples that show how to use MSAL, see the [Azure code samples library](/samples/browse/?products=microsoft-authentication-library).
422
-
- For a Python example of how to create a Batch client authenticated by using an Azure AD token, see the [Deploying Azure Batch Custom Image with a Python Script sample](https://github.com/azurebigcompute/recipes/blob/master/Azure%20Batch/CustomImages/CustomImagePython.md).
423
409
424
410
For more information, see:
425
411
412
+
-[Authenticate Batch Management solutions with Active Directory](batch-aad-auth-management.md)
413
+
-[Client credential flows in MSAL.NET](/entra/msal/dotnet/acquiring-tokens/web-apps-apis/client-credential-flows)
414
+
-[Using MSAL.NET to get tokens by authorization code (for web sites)](/entra/msal/dotnet/acquiring-tokens/web-apps-apis/authorization-codes)
426
415
-[Application and service principal objects in Azure Active Directory](/azure/active-directory/develop/app-objects-and-service-principals)
427
416
-[How to create an Azure AD application and service principal that can access resources](/azure/active-directory/develop/howto-create-service-principal-portal)
428
-
-[Authenticate Batch Management solutions with Active Directory](batch-aad-auth-management.md)
0 commit comments