Share via


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)

메서드별 인증 공급자를 설정합니다.

적용 대상