Skip to content

Commit dbae457

Browse files
committed
updated code snippets
1 parent 0e30e08 commit dbae457

File tree

1 file changed

+31
-45
lines changed

1 file changed

+31
-45
lines changed

articles/service-fabric/service-fabric-connect-to-secure-cluster.md

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -235,23 +235,27 @@ catch (Exception e)
235235
236236
### Connect to a secure cluster non-interactively using Azure Active Directory
237237
238-
The following example relies on Microsoft.IdentityModel.Clients.ActiveDirectory, Version: 2.19.208020213.
239-
240-
> [!IMPORTANT]
241-
> The [Microsoft.IdentityModel.Clients.ActiveDirectory](https://www.nuget.org/packages/Microsoft.IdentityModel.Clients.ActiveDirectory) NuGet package and Azure AD Authentication Library (ADAL) have been deprecated. No new features have been added since June 30, 2020. We strongly encourage you to upgrade, see the [migration guide](../active-directory/develop/msal-migration.md) for more details.
238+
The following example relies on Microsoft.Identity.Client, Version: 4.37.0.
242239
243240
For more information on AAD token acquisition, see [Microsoft.Identity.Client](/dotnet/api/microsoft.identity.client?view=azure-dotnet&preserve-view=true).
244241
245242
```csharp
246243
string tenantId = "C15CFCEA-02C1-40DC-8466-FBD0EE0B05D2";
247244
string clientApplicationId = "118473C2-7619-46E3-A8E4-6DA8D5F56E12";
248245
string webApplicationId = "53E6948C-0897-4DA6-B26A-EE2A38A690B4";
246+
string[] scopes = new string[] { "user.read" };
247+
248+
var pca = PublicClientApplicationBuilder.Create(clientApplicationId)
249+
.WithAuthority($"https://login.microsoftonline.com/{tenantId}")
250+
.WithRedirectUri("urn:ietf:wg:oauth:2.0:oob")
251+
.Build();
249252
250-
string token = GetAccessToken(
251-
tenantId,
252-
webApplicationId,
253-
clientApplicationId,
254-
"urn:ietf:wg:oauth:2.0:oob");
253+
var accounts = await pca.GetAccountsAsync();
254+
var result = await pca.AcquireTokenInteractive(scopes)
255+
.WithAccount(accounts.FirstOrDefault())
256+
.ExecuteAsync();
257+
258+
string token = result.AccessToken;
255259
256260
string serverCertThumb = "A8136758F4AB8962AF2BF3F27921BE1DF67F4326";
257261
string connection = "clustername.westus.cloudapp.azure.com:19000";
@@ -271,26 +275,6 @@ catch (Exception e)
271275
{
272276
Console.WriteLine("Connect failed: {0}", e.Message);
273277
}
274-
275-
...
276-
277-
static string GetAccessToken(
278-
string tenantId,
279-
string resource,
280-
string clientId,
281-
string redirectUri)
282-
{
283-
string authorityFormat = @"https://login.microsoftonline.com/{0}";
284-
string authority = string.Format(CultureInfo.InvariantCulture, authorityFormat, tenantId);
285-
var authContext = new AuthenticationContext(authority);
286-
287-
var authResult = authContext.AcquireToken(
288-
resource,
289-
clientId,
290-
new UserCredential("[email protected]", "TestPassword"));
291-
return authResult.AccessToken;
292-
}
293-
294278
```
295279
296280
### Connect to a secure cluster without prior metadata knowledge using Azure Active Directory
@@ -306,9 +290,25 @@ claimsCredentials.ServerThumbprints.Add(serverCertThumb);
306290
307291
var fc = new FabricClient(claimsCredentials, connection);
308292
309-
fc.ClaimsRetrieval += (o, e) =>
293+
fc.ClaimsRetrieval += async (o, e) =>
310294
{
311-
return GetAccessToken(e.AzureActiveDirectoryMetadata);
295+
var accounts = await PublicClientApplicationBuilder
296+
.Create("<client_id>")
297+
.WithAuthority(AzureCloudInstance.AzurePublic, "<tenant_id>")
298+
.WithRedirectUri("<redirect_uri>")
299+
.Build()
300+
.GetAccountsAsync();
301+
302+
var result = await PublicClientApplicationBuilder
303+
.Create("<client_id>")
304+
.WithAuthority(AzureCloudInstance.AzurePublic, "<tenant_id>")
305+
.WithRedirectUri("<redirect_uri>")
306+
.Build()
307+
.AcquireTokenInteractive(new[] { "<scope>" })
308+
.WithAccount(accounts.FirstOrDefault())
309+
.ExecuteAsync();
310+
311+
return result.AccessToken;
312312
};
313313
314314
try
@@ -320,20 +320,6 @@ catch (Exception e)
320320
{
321321
Console.WriteLine("Connect failed: {0}", e.Message);
322322
}
323-
324-
...
325-
326-
static string GetAccessToken(AzureActiveDirectoryMetadata aad)
327-
{
328-
var authContext = new AuthenticationContext(aad.Authority);
329-
330-
var authResult = authContext.AcquireToken(
331-
aad.ClusterApplication,
332-
aad.ClientApplication,
333-
new UserCredential("[email protected]", "TestPassword"));
334-
return authResult.AccessToken;
335-
}
336-
337323
```
338324
339325
<a id="connectsecureclustersfx"></a>

0 commit comments

Comments
 (0)