Skip to content

Commit c0017eb

Browse files
committed
code
1 parent bf8a2b5 commit c0017eb

File tree

1 file changed

+51
-35
lines changed

1 file changed

+51
-35
lines changed

articles/batch/batch-aad-auth.md

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ You can get your tenant ID from the main Azure AD page in the Azure portal. You
3232

3333
![Screenshot of the Tenant ID in the Azure portal.](./media/batch-aad-auth/aad-directory-id.png)
3434

35-
- The tenant-specific Azure AD endpoint is required when you authenticate by using a service principal.
36-
37-
- When you authenticate by using integrated authentication, the tenant-specific endpoint is optional, but is recommended. You can also use the Azure AD common endpoint to provide a generic credential gathering interface when a specific tenant isn't provided. The common endpoint is `https://login.microsoftonline.com/common`.
38-
39-
For more information about Azure AD endpoints, see [Authentication vs. authorization](/azure/active-directory/develop/authentication-vs-authorization).
35+
>[!IMPORTANT]
36+
>- The tenant-specific Azure AD endpoint is required when you authenticate by using a service principal.
37+
>
38+
>- When you authenticate by using integrated authentication, the tenant-specific endpoint is recommended, but optional. You can also use the Azure AD common endpoint to provide a generic credential gathering interface when a specific tenant isn't provided. The common endpoint is `https://login.microsoftonline.com/common`.
39+
>
40+
>For more information about Azure AD endpoints, see [Authentication vs. authorization](/azure/active-directory/develop/authentication-vs-authorization).
4041
4142
### Batch resource endpoint
4243

@@ -224,30 +225,45 @@ To authenticate with integrated authentication from Batch .NET:
224225
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.
225226

226227
```csharp
227-
public IConfidentialClientApplication CreateApplication()
228-
{
229-
IConfidentialClientApplication app;
230-
231-
app = ConfidentialClientApplicationBuilder.Create(ClientId)
232-
.WithAuthority(AuthorityUri)
233-
.WithRedirectUri(RedirectUri.ToString())
234-
.Build();
235-
return app;
236-
}
237-
238-
// Called from 'code received event'.
239-
public async Task<AuthenticationResult> GetAuthenticationResult(
240-
string BatchResourceId,
241-
string authorizationCode)
228+
public IConfidentialClientApplication CreateApplication()
229+
{
230+
IConfidentialClientApplication app;
231+
232+
app = ConfidentialClientApplicationBuilder.Create(ClientId)
233+
.WithAuthority(AuthorityUri)
234+
.WithRedirectUri(RedirectUri.ToString())
235+
.Build();
236+
237+
return app;
238+
}
239+
240+
// Called from 'code received event'.
241+
public async Task<AuthenticationResult> GetAuthenticationResult(
242+
string authorizationCode)
243+
{
244+
IConfidentialClientApplication app = CreateApplication();
245+
246+
var authResult = await app.AcquireTokenByAuthorizationCode(
247+
new[] { BatchResourceUri },
248+
authorizationCode)
249+
.ExecuteAsync()
250+
.ConfigureAwait(false);
251+
252+
return authResult;
253+
}
254+
```
255+
256+
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:
257+
258+
```csharp
259+
public static void PerformBatchOperations()
242260
{
243-
IConfidentialClientApplication app = CreateApplication();
244-
245-
var authResult = await app.AcquireTokenByAuthorizationCode(
246-
new[] { $"BatchResourceUri/.default" },
247-
authorizationCode)
248-
.ExecuteAsync()
261+
Func<Task<string>> tokenProvider = () => GetAuthenticationTokenAsync();
249262

250-
return authResult;
263+
using (var client = BatchClient.Open(new BatchTokenCredentials(BatchAccountUrl, tokenProvider)))
264+
{
265+
client.JobOperations.ListJobs();
266+
}
251267
}
252268
```
253269

@@ -300,14 +316,14 @@ To authenticate with a service principal from Batch .NET:
300316
```csharp
301317
public IConfidentialClientApplication CreateApplication()
302318
{
303-
IConfidentialClientApplication app;
304-
305-
{
306-
app = ConfidentialClientApplicationBuilder.Create(ClientId)
307-
.WithSecret(ClientKey)
308-
.WithAuthority(AuthorityUri)
309-
.Build();
310-
}
319+
IConfidentialClientApplication app;
320+
321+
{
322+
app = ConfidentialClientApplicationBuilder.Create(ClientId)
323+
.WithSecret(ClientKey)
324+
.WithAuthority(AuthorityUri)
325+
.Build();
326+
}
311327

312328
// Called from 'code received event'.
313329
public async Task<AuthenticationResult> GetAuthenticationResult(

0 commit comments

Comments
 (0)