Partager via


Comment déterminer si le type d’authentification est Kerberos

Cet article fournit une requête pour vous aider à déterminer le type d’authentification utilisé lorsque vous vous connectez à Microsoft SQL Server. Veillez à exécuter la requête sur un ordinateur client, et non sur le serveur SQL Server que vous testez. Sinon, la requête retourne auth_scheme en tant que NTLM même si Kerberos est configuré correctement. Cela se produit en raison d’une fonctionnalité de renforcement de la sécurité SID par service qui a été ajoutée dans Windows 2008. Cette fonctionnalité force toutes les connexions locales à utiliser NTLM, que Kerberos soit disponible ou non.

 SELECT auth_scheme FROM sys.dm_exec_connections WHERE session_id=@@SPID

Utiliser SQL Server Management Studio

Exécutez la requête suivante dans SQL Server Management Studio :

SELECT c.session_id, c.net_transport, c.encrypt_option,
       c.auth_scheme, s.host_name, @@SERVERNAME as "remote_name",
       s.program_name, s.client_interface_name, s.login_name,
       s.nt_domain, s.nt_user_name, s.original_login_name,
       c.connect_time, s.login_time
FROM sys.dm_exec_connections AS c
JOIN sys.dm_exec_sessions AS s ON c.session_id = s.session_id
WHERE c.session_id=@@SPID

Utiliser la ligne de commande

Exécutez la requête suivante à l’invite de commandes :

C:\Temp>sqlcmd -S SQLProd01 -E -Q "select auth_scheme from sys.dm_exec_connections where session_id=@@SPID"
auth_scheme
----------------------------------------
NTLM

(1 rows affected)

Autre méthode

Si l’une des options précédentes n’est pas disponible, envisagez d’utiliser la procédure alternative suivante :

  1. Copiez le script suivant dans un éditeur de texte, tel que le Bloc-notes, et enregistrez-le sousgetAuthScheme.vbs:

    ' Auth scheme VB script.
    ' Run on a client machine, not the server.
    ' If you run locally, you will always get NTLM even if Kerberos is properly enabled.
    '
    ' USAGE:  CSCRIPT getAuthScheme.vbs tcp:SQLProd01.contoso.com,1433   ' explicitly specify DNS suffix, protocol, and port # ('tcp' must be lower case)
    ' USAGE:  CSCRIPT getAuthScheme.vbs SQLProd01                        ' let the driver figure out the DNS suffix, protocol, and port #
    '
    Dim cn, rs, s
    s = WScript.Arguments.Item(0)              ' get the server name from the command-line
    Set cn = createobject("adodb.connection")
    '
    ' Various connection strings depending on the driver/Provider installed on your machine
    ' SQLOLEDB is selected as it is on all windows machines, but may have limitations, such as lack of TLS 1.2 support
    ' Choose a newer provider or driver if you have it installed.
    '
    cn.open "Provider=SQLOLEDB;Data Source=" & s & ";Initial Catalog=master;Integrated Security=SSPI"          ' On all Windows machines
    'cn.open "Provider=SQLNCLI11;Data Source=" & s & ";Initial Catalog=master;Integrated Security=SSPI"        ' Newer
    'cn.open "Provider=MSOLEDBSQL;Data Source=" & s & ";Initial Catalog=master;Integrated Security=SSPI"       ' Latest, good for SQL 2012 and newer
    'cn.open "Driver={ODBC Driver 17 for SQL Server};Server=" & s & ";Database=master;Trusted_Connection=Yes"  ' Latest
    '
    ' Run the query and display the results
    '
    set rs = cn.Execute("select auth_scheme from sys.dm_exec_connections where session_id=@@SPID")
    WScript.Echo "Auth scheme: " & rs(0)
    rs.close
    cn.close
    
  2. Exécutez le script PowerShell getAuthScheme.vbs à partir d’une invite de commandes :

    C:\Temp>cscript getAuthScheme.vbs SQLProd01
    

    La sortie suivante doit s’afficher :

    Microsoft (R) Windows Script Host Version 5.812
    Copyright (C) Microsoft Corporation. All rights reserved.
    Auth scheme: NTLM
    

Utiliser PowerShell

Vous pouvez utiliser PowerShell pour tester le fournisseur .NET SqlClient afin d’essayer d’isoler le problème de votre application :

  1. Copiez le script suivant dans un éditeur de texte, tel que le Bloc-notes, et enregistrez-le sousget-SqlAuthScheme.ps1.

  2. Exécutez le script suivant à une invite de commandes :

    #-------------------------------
    #
    # get-SqlAuthScheme.ps1
    #
    # PowerShell script to test a System.Data.SqlClient database connection
    #
    # USAGE: .\get-SqlAuthScheme tcp:SQLProd01.contoso.com,1433   ' explicitly specify DNS suffix,  protocol, and port # ('tcp' must be lower case)
    # USAGE: .\get-SqlAuthScheme SQLProd01                        ' let the driver figure out the DNS suffix, protocol, and port #
    #
    #-------------------------------
    param ([string]$server = "localhost")
    Set-ExecutionPolicy Unrestricted-Scope CurrentUser
    $connstr = "Server=$server;Database=master;Integrated Security=SSPI"
    [System.Data.SqlClient.SqlConnection] $conn = New-Object System.Data.SqlClient.SqlConnection
    $conn.ConnectionString = $connstr
    [System.DateTime] $start = Get-Date
    $conn.Open()
    [System.Data.SqlClient.SqlCommand] $cmd = New-Object System.Data.SqlClient.SqlCommand
    $cmd.CommandText = "select auth_scheme from sys.dm_exec_connections where session_id=@@spid"
    $cmd.Connection = $conn
    $dr = $cmd.ExecuteReader()
    $result = $dr.Read()
    $auth_scheme = $dr.GetString(0)
    $conn.Close()
    $conn.Dispose()
    [System.DateTime] $end = Get-Date
    [System.Timespan] $span = ($end - $start)
    "End time: " + $end.ToString("M/d/yyyy HH:mm:ss.fff")
    "Elapsed time was " + $span.Milliseconds + " ms."
    "Auth scheme for " + $server + ": " + $auth_scheme
    

La sortie suivante doit s’afficher :

C:\temp> .\get-sqlauthscheme sqlprod01
End time: 10/26/2020 18:00:24.753
Elapsed time was 0 ms.
Auth scheme for sqlprod01: NTLM

Informations supplémentaires

Problèmes d’authentification cohérente dans SQL Server