-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reverting removal of the snippet files. (#2835)
- Loading branch information
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//<Snippet1> | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Identity.Client; | ||
using Microsoft.Data.SqlClient; | ||
|
||
namespace CustomAuthenticationProviderExamples | ||
{ | ||
public class Program | ||
{ | ||
public static void Main() | ||
{ | ||
SqlAuthenticationProvider authProvider = new ActiveDirectoryAuthenticationProvider(CustomDeviceFlowCallback); | ||
SqlAuthenticationProvider.SetProvider(SqlAuthenticationMethod.ActiveDirectoryDeviceCodeFlow, authProvider); | ||
using (SqlConnection sqlConnection = new SqlConnection("Server=<myserver>.database.windows.net;Authentication=Active Directory Device Code Flow;Database=<db>;")) | ||
{ | ||
sqlConnection.Open(); | ||
Console.WriteLine("Connected successfully!"); | ||
} | ||
} | ||
|
||
private static Task CustomDeviceFlowCallback(DeviceCodeResult result) | ||
{ | ||
// Provide custom logic to process result information and read device code. | ||
Console.WriteLine(result.Message); | ||
return Task.FromResult(0); | ||
} | ||
} | ||
} | ||
//</Snippet1> |
26 changes: 26 additions & 0 deletions
26
doc/samples/ApplicationClientIdAzureAuthenticationProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//<Snippet1> | ||
using System; | ||
using Microsoft.Data.SqlClient; | ||
|
||
namespace CustomAuthenticationProviderExamples | ||
{ | ||
public class Program | ||
{ | ||
public static void Main() | ||
{ | ||
// Supported for all authentication modes supported by ActiveDirectoryAuthenticationProvider | ||
ActiveDirectoryAuthenticationProvider provider = new ActiveDirectoryAuthenticationProvider("<application_client_id>"); | ||
if (provider.IsSupported(SqlAuthenticationMethod.ActiveDirectoryInteractive)) | ||
{ | ||
SqlAuthenticationProvider.SetProvider(SqlAuthenticationMethod.ActiveDirectoryInteractive, provider); | ||
} | ||
|
||
using (SqlConnection sqlConnection = new SqlConnection("Server=<myserver>.database.windows.net;Authentication=Active Directory Interactive;Database=<db>;")) | ||
{ | ||
sqlConnection.Open(); | ||
Console.WriteLine("Connected successfully!"); | ||
} | ||
} | ||
} | ||
} | ||
//</Snippet1> |
57 changes: 57 additions & 0 deletions
57
doc/samples/CustomDeviceCodeFlowAzureAuthenticationProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//<Snippet1> | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Identity.Client; | ||
using Microsoft.Data.SqlClient; | ||
|
||
namespace CustomAuthenticationProviderExamples | ||
{ | ||
/// <summary> | ||
/// Example demonstrating creating a custom device code flow authentication provider and attaching it to the driver. | ||
/// This is helpful for applications that wish to override the Callback for the Device Code Result implemented by the SqlClient driver. | ||
/// </summary> | ||
public class CustomDeviceCodeFlowAzureAuthenticationProvider : SqlAuthenticationProvider | ||
{ | ||
public override async Task<SqlAuthenticationToken> AcquireTokenAsync(SqlAuthenticationParameters parameters) | ||
{ | ||
string clientId = "my-client-id"; | ||
string clientName = "My Application Name"; | ||
string s_defaultScopeSuffix = "/.default"; | ||
|
||
string[] scopes = new string[] { parameters.Resource.EndsWith(s_defaultScopeSuffix) ? parameters.Resource : parameters.Resource + s_defaultScopeSuffix }; | ||
|
||
IPublicClientApplication app = PublicClientApplicationBuilder.Create(clientId) | ||
.WithAuthority(parameters.Authority) | ||
.WithClientName(clientName) | ||
.WithRedirectUri("https://login.microsoftonline.com/common/oauth2/nativeclient") | ||
.Build(); | ||
|
||
AuthenticationResult result = await app.AcquireTokenWithDeviceCode(scopes, | ||
deviceCodeResult => CustomDeviceFlowCallback(deviceCodeResult)).ExecuteAsync(); | ||
return new SqlAuthenticationToken(result.AccessToken, result.ExpiresOn); | ||
} | ||
|
||
public override bool IsSupported(SqlAuthenticationMethod authenticationMethod) => authenticationMethod.Equals(SqlAuthenticationMethod.ActiveDirectoryDeviceCodeFlow); | ||
|
||
private Task CustomDeviceFlowCallback(DeviceCodeResult result) | ||
{ | ||
Console.WriteLine(result.Message); | ||
return Task.FromResult(0); | ||
} | ||
} | ||
|
||
public class Program | ||
{ | ||
public static void Main() | ||
{ | ||
// Register our custom authentication provider class to override Active Directory Device Code Flow | ||
SqlAuthenticationProvider.SetProvider(SqlAuthenticationMethod.ActiveDirectoryDeviceCodeFlow, new CustomDeviceCodeFlowAzureAuthenticationProvider()); | ||
using (SqlConnection sqlConnection = new SqlConnection("Server=<myserver>.database.windows.net;Authentication=Active Directory Device Code Flow;Database=<db>;")) | ||
{ | ||
sqlConnection.Open(); | ||
Console.WriteLine("Connected successfully!"); | ||
} | ||
} | ||
} | ||
} | ||
//</Snippet1> |