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) |
依方法設定驗證提供者。 |