SqlAuthenticationProvider 类

定义

定义验证提供程序的核心行为,并为派生类提供基类。

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)

按方法设置验证提供程序。

适用于