SqlAuthenticationProvider クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
認証プロバイダーのコア動作を定義し、派生クラスの基底クラスを提供します。
public ref class SqlAuthenticationProvider abstract
public abstract class SqlAuthenticationProvider
type SqlAuthenticationProvider = class
Public MustInherit Class SqlAuthenticationProvider
- 継承
-
SqlAuthenticationProvider
- 派生
例
次の例では、カスタム SqlAuthenticationProvider を実装し、デバイス コード フロー認証モードをオーバーライドするために SqlClient に同じものを提供する方法を示します。
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!");
}
}
}
}
注釈
派生クラスは、app.config ファイルからインスタンス化できる場合は、パラメーターなしのコンストラクターを提供する必要があります。
コンストラクター
SqlAuthenticationProvider() |
認証プロバイダーのコア動作を定義し、派生クラスの基底クラスを提供します。 |
メソッド
AcquireTokenAsync(SqlAuthenticationParameters) |
機関からセキュリティ トークンを取得します。 |
BeforeLoad(SqlAuthenticationMethod) |
このメソッドは、プロバイダーが SQL ドライバーのレジストリに追加される直前に呼び出されます。 |
BeforeUnload(SqlAuthenticationMethod) |
このメソッドは、プロバイダーが SQL ドライバーのレジストリから削除される直前に呼び出されます。 |
GetProvider(SqlAuthenticationMethod) |
メソッドで認証プロバイダーを取得します。 |
IsSupported(SqlAuthenticationMethod) |
指定された認証方法がサポートされているかどうかを示します。 |
SetProvider(SqlAuthenticationMethod, SqlAuthenticationProvider) |
メソッドで認証プロバイダーを設定します。 |