Udostępnij za pośrednictwem


SqlAuthenticationProvider Klasa

Definicja

Definiuje podstawowe zachowanie dostawców uwierzytelniania i udostępnia klasę bazową dla klas pochodnych.

public ref class SqlAuthenticationProvider abstract
public abstract class SqlAuthenticationProvider
type SqlAuthenticationProvider = class
Public MustInherit Class SqlAuthenticationProvider
Dziedziczenie
SqlAuthenticationProvider
Pochodne

Przykłady

W poniższym przykładzie pokazano implementację niestandardowego obiektu SqlAuthenticationProvider i udostępnienie tego samego elementu sqlClient w celu zastąpienia trybu uwierzytelniania przepływu kodu urządzenia:

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!");
            }
        }
    }
}

Uwagi

Klasy pochodne muszą dostarczyć konstruktora bez parametrów, jeśli można utworzyć wystąpienie z pliku app.config.

Konstruktory

SqlAuthenticationProvider()

Definiuje podstawowe zachowanie dostawców uwierzytelniania i udostępnia klasę bazową dla klas pochodnych.

Metody

AcquireTokenAsync(SqlAuthenticationParameters)

Uzyskuje token zabezpieczający z urzędu.

BeforeLoad(SqlAuthenticationMethod)

Ta metoda jest wywoływana bezpośrednio przed dodaniu dostawcy do rejestru sterowników SQL.

BeforeUnload(SqlAuthenticationMethod)

Ta metoda jest wywoływana bezpośrednio przed usunięciem dostawcy z rejestru sterowników SQL.

GetProvider(SqlAuthenticationMethod)

Pobiera dostawcę uwierzytelniania według metody.

IsSupported(SqlAuthenticationMethod)

Wskazuje, czy określona metoda uwierzytelniania jest obsługiwana.

SetProvider(SqlAuthenticationMethod, SqlAuthenticationProvider)

Ustawia dostawcę uwierzytelniania według metody.

Dotyczy