How to get Windows Log-in User Name for a SQL Log in User

75,552

Solution 1

There is no link between a SQL login and the NT username.

You asked similar here: How to find out user name and machine name to access to SQL server

The WMI approach will be ambiguous if more than 1 user is logged into the client PC (eg service accounts, remote user via mstsc etc). Any approach like this will require admin rights on the client PC too for the account used.

I doubt you can do it in real time.

All you can do is record the client_net_address in sys.dm_exec_connections and backtrack from there, perhaps via WMI but not from SQL Server itself.

Do you need the username though? Or just the client PC so you can change the app connection string?

You final solution is to change the sa password and see who calls, if you only have relatively few SQL connections

Solution 2

To get the user and machine name use this:

SELECT HOST_NAME() AS HostName, SUSER_NAME() LoggedInUser

Solution 3

  1. You can get the client ip address and remote PID from querying sessions & connections.
  2. Use this info to build a TASKLIST command.
  3. Use XP_CMDShell to execute the built command to get the user.

    DECLARE @CMD VARCHAR(500) = 
    (SELECT  TOP 1 'tasklist /S ' + client_net_address + 
    ' /FI "PID eq ' + CONVERT(VARCHAR(MAX),host_process_id) + 
    '" /V /FO LIST /U DOMAIN\Admin /P password' 
    FROM sys.dm_exec_connections C JOIN sys.dm_exec_sessions S
    ON C.session_id = S.session_id WHERE S.session_id = @@SPID)
    EXEC xp_cmdshell @CMD

You can use it as you please. Either to send a mail to DBA by using it in an ALL SERVER trigger or for Ad-Hoc auditing. Hope this helps =)

Share:
75,552
David.Chu.ca
Author by

David.Chu.ca

Updated on February 01, 2020

Comments

  • David.Chu.ca
    David.Chu.ca over 4 years

    By using sp_who2 I can get a list of current log-in users and machine names. Some users are using SQL Server log-in user name such as sa. Is there any way I can get the windows log-in user name for the SQL log-in users?

    If there is no way to get Windows log-in users from the machine names, can I use WMI class or C# to get find out the Windows log-in user names by their machine names?

    My SQL server is Microsoft SQL Server 2005 and Windows is Server 2003.

  • David.Chu.ca
    David.Chu.ca about 15 years
    yes. I have to find another way to figure out user name such as a new service from IT to get default user name for a machine and current user.
  • user007
    user007 almost 9 years
    This will not give the NTID which the OP is asking.
  • StingyJack
    StingyJack about 8 years
    This does give the windows user name (Domain\UserName) that OP is asking for. It doesn't give an NT SID.
  • underscore_d
    underscore_d over 7 years
    No, this can only give the NT username iff the user is logged into the SQL Server using Windows Authentication. Reading the OP again will show that they wanted somehow to get the NT name for people connected using SQL logins.