|
8 | 8 | <example> |
9 | 9 | The following example demonstrates implementing a custom SqlAuthenticationProvider and providing the same to SqlClient for overriding Device Code Flow authentication mode: |
10 | 10 | <code language="c#"> |
11 | | - <![CDATA[ |
12 | | -using System; |
13 | | -using System.Threading.Tasks; |
14 | | -using Microsoft.Identity.Client; |
15 | | -using Microsoft.Data.SqlClient; |
16 | | -
|
17 | | -namespace CustomAuthenticationProviderExamples |
18 | | -{ |
19 | | - /// summary> |
20 | | - /// Example demonstrating creating a custom device code flow authentication provider and attaching it to the driver. |
21 | | - /// This is helpful for applications that wish to override the Callback for the Device Code Result implemented by the SqlClient driver. |
22 | | - /// </summary> |
23 | | - public class CustomDeviceCodeFlowAzureAuthenticationProvider : SqlAuthenticationProvider |
24 | | - { |
25 | | - public override async Task<SqlAuthenticationToken> AcquireTokenAsync(SqlAuthenticationParameters parameters) |
26 | | - { |
27 | | - string clientId = "my-client-id"; |
28 | | - string clientName = "My Application Name"; |
29 | | - string s_defaultScopeSuffix = "/.default"; |
30 | | -
|
31 | | - string[] scopes = new string[] { parameters.Resource.EndsWith(s_defaultScopeSuffix) ? parameters.Resource : parameters.Resource + s_defaultScopeSuffix }; |
32 | | -
|
33 | | - IPublicClientApplication app = PublicClientApplicationBuilder.Create(clientId) |
34 | | - .WithAuthority(parameters.Authority) |
35 | | - .WithClientName(clientName) |
36 | | - .WithRedirectUri("https://login.microsoftonline.com/common/oauth2/nativeclient") |
37 | | - .Build(); |
38 | | -
|
39 | | - AuthenticationResult result = await app.AcquireTokenWithDeviceCode(scopes, |
40 | | - deviceCodeResult => CustomDeviceFlowCallback(deviceCodeResult)).ExecuteAsync(); |
41 | | - return new SqlAuthenticationToken(result.AccessToken, result.ExpiresOn); |
42 | | - } |
43 | | -
|
44 | | - public override bool IsSupported(SqlAuthenticationMethod authenticationMethod) => |
45 | | - authenticationMethod.Equals(SqlAuthenticationMethod.ActiveDirectoryDeviceCodeFlow); |
46 | | -
|
47 | | - private Task CustomDeviceFlowCallback(DeviceCodeResult result) |
48 | | - { |
49 | | - Console.WriteLine(result.Message); |
50 | | - return Task.FromResult(0); |
51 | | - } |
52 | | - } |
53 | | -
|
54 | | - public class Program |
55 | | - { |
56 | | - public static void Main() |
57 | | - { |
58 | | - // Register our custom authentication provider class to override Active Directory Device Code Flow |
59 | | - SqlAuthenticationProvider.SetProvider(SqlAuthenticationMethod.ActiveDirectoryDeviceCodeFlow, new CustomDeviceCodeFlowAzureAuthenticationProvider()); |
60 | | - using (SqlConnection sqlConnection = new SqlConnection("Server=<myserver>.database.windows.net;Authentication=Active Directory Device Code Flow;Database=<db>;")) |
61 | | - { |
62 | | - sqlConnection.Open(); |
63 | | - Console.WriteLine("Connected successfully!"); |
64 | | - } |
65 | | - } |
66 | | - } |
67 | | -} |
68 | | - ]]> |
| 11 | + using System; |
| 12 | + using System.Threading.Tasks; |
| 13 | + using Microsoft.Identity.Client; |
| 14 | + using Microsoft.Data.SqlClient; |
| 15 | + |
| 16 | + namespace CustomAuthenticationProviderExamples |
| 17 | + { |
| 18 | + /// summary> |
| 19 | + /// Example demonstrating creating a custom device code flow authentication provider and attaching it to the driver. |
| 20 | + /// This is helpful for applications that wish to override the Callback for the Device Code Result implemented by the SqlClient driver. |
| 21 | + /// </summary> |
| 22 | + public class CustomDeviceCodeFlowAzureAuthenticationProvider : SqlAuthenticationProvider |
| 23 | + { |
| 24 | + public override async Task<SqlAuthenticationToken> AcquireTokenAsync(SqlAuthenticationParameters parameters) |
| 25 | + { |
| 26 | + string clientId = "my-client-id"; |
| 27 | + string clientName = "My Application Name"; |
| 28 | + string s_defaultScopeSuffix = "/.default"; |
| 29 | + |
| 30 | + 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 | + .Build(); |
| 37 | + |
| 38 | + AuthenticationResult result = await app.AcquireTokenWithDeviceCode(scopes, |
| 39 | + deviceCodeResult => CustomDeviceFlowCallback(deviceCodeResult)).ExecuteAsync(); |
| 40 | + return new SqlAuthenticationToken(result.AccessToken, result.ExpiresOn); |
| 41 | + } |
| 42 | + |
| 43 | + public override bool IsSupported(SqlAuthenticationMethod authenticationMethod) => |
| 44 | + authenticationMethod.Equals(SqlAuthenticationMethod.ActiveDirectoryDeviceCodeFlow); |
| 45 | + |
| 46 | + private Task CustomDeviceFlowCallback(DeviceCodeResult result) |
| 47 | + { |
| 48 | + Console.WriteLine(result.Message); |
| 49 | + return Task.FromResult(0); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public class Program |
| 54 | + { |
| 55 | + public static void Main() |
| 56 | + { |
| 57 | + // Register our custom authentication provider class to override Active Directory Device Code Flow |
| 58 | + SqlAuthenticationProvider.SetProvider(SqlAuthenticationMethod.ActiveDirectoryDeviceCodeFlow, new CustomDeviceCodeFlowAzureAuthenticationProvider()); |
| 59 | + using (SqlConnection sqlConnection = new SqlConnection("Server=<myserver>.database.windows.net;Authentication=Active Directory Device Code Flow;Database=<db>;")) |
| 60 | + { |
| 61 | + sqlConnection.Open(); |
| 62 | + Console.WriteLine("Connected successfully!"); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
69 | 67 | </code> |
70 | 68 | </example> |
71 | 69 | </SqlAuthenticationProvider> |
|
0 commit comments