Share via


ActiveDirectoryAuthenticationProvider.SetDeviceCodeFlowCallback 方法

定义

设置回调方法,重写处理“Active Directory 设备代码流”身份验证结果的默认实现。

public:
 void SetDeviceCodeFlowCallback(Func<Microsoft::Identity::Client::DeviceCodeResult ^, System::Threading::Tasks::Task ^> ^ deviceCodeFlowCallbackMethod);
public void SetDeviceCodeFlowCallback (Func<Microsoft.Identity.Client.DeviceCodeResult,System.Threading.Tasks.Task> deviceCodeFlowCallbackMethod);
member this.SetDeviceCodeFlowCallback : Func<Microsoft.Identity.Client.DeviceCodeResult, System.Threading.Tasks.Task> -> unit
Public Sub SetDeviceCodeFlowCallback (deviceCodeFlowCallbackMethod As Func(Of DeviceCodeResult, Task))

参数

deviceCodeFlowCallbackMethod
Func<DeviceCodeResult,Task>

要用于“Active Directory 设备代码流”身份验证的回调方法。

示例

以下示例演示如何为设备代码流身份验证方法向 SqlClient 提供自定义设备流回调:

using System;
using System.Threading.Tasks;
using Microsoft.Identity.Client;
using Microsoft.Data.SqlClient;

namespace CustomAuthenticationProviderExamples
{
    public class Program
    {
        public static void Main()
        {
            SqlAuthenticationProvider authProvider = new ActiveDirectoryAuthenticationProvider(CustomDeviceFlowCallback);
            SqlAuthenticationProvider.SetProvider(SqlAuthenticationMethod.ActiveDirectoryDeviceCodeFlow, authProvider);
            using (SqlConnection sqlConnection = new SqlConnection("Server=<myserver>.database.windows.net;Authentication=Active Directory Device Code Flow;Database=<db>;"))
            {
                sqlConnection.Open();
                Console.WriteLine("Connected successfully!");
            }
        }

        private static Task CustomDeviceFlowCallback(DeviceCodeResult result)
        {
            // Provide custom logic to process result information and read device code.
            Console.WriteLine(result.Message);
            return Task.FromResult(0);
        }
    }
}

适用于