Skip to content

Commit cdc33d9

Browse files
authored
Sync sample changes into snippet doc (#2837)
1 parent 68a5be4 commit cdc33d9

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

doc/snippets/Microsoft.Data.SqlClient/SqlAuthenticationProvider.xml

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
The following example demonstrates implementing a custom SqlAuthenticationProvider and providing the same to SqlClient for overriding Device Code Flow authentication mode:
1010
<code language="c#">
1111
using System;
12+
using System.Collections.Generic;
13+
using System.Linq;
1214
using System.Threading.Tasks;
13-
using Microsoft.Identity.Client;
1415
using Microsoft.Data.SqlClient;
15-
16+
using Microsoft.Identity.Client;
17+
1618
namespace CustomAuthenticationProviderExamples
1719
{
1820
/// &lt;summary&gt;
@@ -21,35 +23,52 @@
2123
/// &lt;/summary&gt;
2224
public class CustomDeviceCodeFlowAzureAuthenticationProvider : SqlAuthenticationProvider
2325
{
26+
private const string clientId = "my-client-id";
27+
private const string clientName = "My Application Name";
28+
private const string s_defaultScopeSuffix = "/.default";
29+
30+
// Maintain a copy of the PublicClientApplication object to cache the underlying access tokens it provides
31+
private static IPublicClientApplication pcApplication;
32+
2433
public override async Task&lt;SqlAuthenticationToken&gt; AcquireTokenAsync(SqlAuthenticationParameters parameters)
2534
{
26-
string clientId = "my-client-id";
27-
string clientName = "My Application Name";
28-
string s_defaultScopeSuffix = "/.default";
29-
3035
string[] scopes = new string[] { parameters.Resource.EndsWith(s_defaultScopeSuffix) ? parameters.Resource : parameters.Resource + s_defaultScopeSuffix };
31-
32-
IPublicClientApplication app = PublicClientApplicationBuilder.Create(clientId)
33-
.WithAuthority(parameters.Authority)
34-
.WithClientName(clientName)
35-
.WithRedirectUri("https://login.microsoftonline.com/common/oauth2/nativeclient")
36+
37+
IPublicClientApplication app = pcApplication;
38+
if (app == null)
39+
{
40+
pcApplication = app = PublicClientApplicationBuilder.Create(clientId)
41+
.WithAuthority(parameters.Authority)
42+
.WithClientName(clientName)
43+
.WithRedirectUri("https://login.microsoftonline.com/common/oauth2/nativeclient")
3644
.Build();
37-
38-
AuthenticationResult result = await app.AcquireTokenWithDeviceCode(scopes,
39-
deviceCodeResult => CustomDeviceFlowCallback(deviceCodeResult)).ExecuteAsync();
45+
}
46+
47+
AuthenticationResult result;
48+
49+
try
50+
{
51+
IEnumerable&lt;IAccount&gt; accounts = await app.GetAccountsAsync();
52+
result = await app.AcquireTokenSilent(scopes, accounts.FirstOrDefault()).ExecuteAsync();
53+
}
54+
catch (MsalUiRequiredException)
55+
{
56+
result = await app.AcquireTokenWithDeviceCode(scopes,
57+
deviceCodeResult => CustomDeviceFlowCallback(deviceCodeResult)).ExecuteAsync();
58+
}
59+
4060
return new SqlAuthenticationToken(result.AccessToken, result.ExpiresOn);
4161
}
42-
43-
public override bool IsSupported(SqlAuthenticationMethod authenticationMethod) =>
44-
authenticationMethod.Equals(SqlAuthenticationMethod.ActiveDirectoryDeviceCodeFlow);
45-
46-
private Task CustomDeviceFlowCallback(DeviceCodeResult result)
62+
63+
public override bool IsSupported(SqlAuthenticationMethod authenticationMethod) => authenticationMethod.Equals(SqlAuthenticationMethod.ActiveDirectoryDeviceCodeFlow);
64+
65+
private static Task&lt;int&gt; CustomDeviceFlowCallback(DeviceCodeResult result)
4766
{
4867
Console.WriteLine(result.Message);
4968
return Task.FromResult(0);
5069
}
5170
}
52-
71+
5372
public class Program
5473
{
5574
public static void Main()

0 commit comments

Comments
 (0)