Skip to content

Commit 5dc7b06

Browse files
authored
Reverting removal of the snippet files. (#2835)
1 parent b6a28f7 commit 5dc7b06

3 files changed

+113
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//<Snippet1>
2+
using System;
3+
using System.Threading.Tasks;
4+
using Microsoft.Identity.Client;
5+
using Microsoft.Data.SqlClient;
6+
7+
namespace CustomAuthenticationProviderExamples
8+
{
9+
public class Program
10+
{
11+
public static void Main()
12+
{
13+
SqlAuthenticationProvider authProvider = new ActiveDirectoryAuthenticationProvider(CustomDeviceFlowCallback);
14+
SqlAuthenticationProvider.SetProvider(SqlAuthenticationMethod.ActiveDirectoryDeviceCodeFlow, authProvider);
15+
using (SqlConnection sqlConnection = new SqlConnection("Server=<myserver>.database.windows.net;Authentication=Active Directory Device Code Flow;Database=<db>;"))
16+
{
17+
sqlConnection.Open();
18+
Console.WriteLine("Connected successfully!");
19+
}
20+
}
21+
22+
private static Task CustomDeviceFlowCallback(DeviceCodeResult result)
23+
{
24+
// Provide custom logic to process result information and read device code.
25+
Console.WriteLine(result.Message);
26+
return Task.FromResult(0);
27+
}
28+
}
29+
}
30+
//</Snippet1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//<Snippet1>
2+
using System;
3+
using Microsoft.Data.SqlClient;
4+
5+
namespace CustomAuthenticationProviderExamples
6+
{
7+
public class Program
8+
{
9+
public static void Main()
10+
{
11+
// Supported for all authentication modes supported by ActiveDirectoryAuthenticationProvider
12+
ActiveDirectoryAuthenticationProvider provider = new ActiveDirectoryAuthenticationProvider("<application_client_id>");
13+
if (provider.IsSupported(SqlAuthenticationMethod.ActiveDirectoryInteractive))
14+
{
15+
SqlAuthenticationProvider.SetProvider(SqlAuthenticationMethod.ActiveDirectoryInteractive, provider);
16+
}
17+
18+
using (SqlConnection sqlConnection = new SqlConnection("Server=<myserver>.database.windows.net;Authentication=Active Directory Interactive;Database=<db>;"))
19+
{
20+
sqlConnection.Open();
21+
Console.WriteLine("Connected successfully!");
22+
}
23+
}
24+
}
25+
}
26+
//</Snippet1>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//<Snippet1>
2+
using System;
3+
using System.Threading.Tasks;
4+
using Microsoft.Identity.Client;
5+
using Microsoft.Data.SqlClient;
6+
7+
namespace CustomAuthenticationProviderExamples
8+
{
9+
/// <summary>
10+
/// Example demonstrating creating a custom device code flow authentication provider and attaching it to the driver.
11+
/// This is helpful for applications that wish to override the Callback for the Device Code Result implemented by the SqlClient driver.
12+
/// </summary>
13+
public class CustomDeviceCodeFlowAzureAuthenticationProvider : SqlAuthenticationProvider
14+
{
15+
public override async Task<SqlAuthenticationToken> AcquireTokenAsync(SqlAuthenticationParameters parameters)
16+
{
17+
string clientId = "my-client-id";
18+
string clientName = "My Application Name";
19+
string s_defaultScopeSuffix = "/.default";
20+
21+
string[] scopes = new string[] { parameters.Resource.EndsWith(s_defaultScopeSuffix) ? parameters.Resource : parameters.Resource + s_defaultScopeSuffix };
22+
23+
IPublicClientApplication app = PublicClientApplicationBuilder.Create(clientId)
24+
.WithAuthority(parameters.Authority)
25+
.WithClientName(clientName)
26+
.WithRedirectUri("https://login.microsoftonline.com/common/oauth2/nativeclient")
27+
.Build();
28+
29+
AuthenticationResult result = await app.AcquireTokenWithDeviceCode(scopes,
30+
deviceCodeResult => CustomDeviceFlowCallback(deviceCodeResult)).ExecuteAsync();
31+
return new SqlAuthenticationToken(result.AccessToken, result.ExpiresOn);
32+
}
33+
34+
public override bool IsSupported(SqlAuthenticationMethod authenticationMethod) => authenticationMethod.Equals(SqlAuthenticationMethod.ActiveDirectoryDeviceCodeFlow);
35+
36+
private Task CustomDeviceFlowCallback(DeviceCodeResult result)
37+
{
38+
Console.WriteLine(result.Message);
39+
return Task.FromResult(0);
40+
}
41+
}
42+
43+
public class Program
44+
{
45+
public static void Main()
46+
{
47+
// Register our custom authentication provider class to override Active Directory Device Code Flow
48+
SqlAuthenticationProvider.SetProvider(SqlAuthenticationMethod.ActiveDirectoryDeviceCodeFlow, new CustomDeviceCodeFlowAzureAuthenticationProvider());
49+
using (SqlConnection sqlConnection = new SqlConnection("Server=<myserver>.database.windows.net;Authentication=Active Directory Device Code Flow;Database=<db>;"))
50+
{
51+
sqlConnection.Open();
52+
Console.WriteLine("Connected successfully!");
53+
}
54+
}
55+
}
56+
}
57+
//</Snippet1>

0 commit comments

Comments
 (0)