views:

151

answers:

2

Hello,

I've been stuck on this issue for several days - any help is much appreciated.

I have an SQL trigger that's calling a DLL written in C#. In this function, I require the windows login name of the person that originated the SQL command that fired the trigger. It doesn't matter whether I get this info from C# or SQL.

Unfortunately most of the normal routes have failed on me.

Built in SQL function NT_CLIENT() only works if the SQL login is using Windows Authentication.

The DLL is running on the server, and therefore Environment.UserName isn't relevant and WindowsIdentity.GetCurrent().Name returns an empty string unless the database user (thread originator) was using Windows Authentication.

I know the domain name, the ip address, and the workstation name. From this I've figured out how to get the SID in C# - is there anyway I can get the Windows login name from those bits of information using C# or is there an SQL built in function that I missed?

EDIT: Thanks for letting me know SQL was a dud for this problem. What we have is a client that logs everyone into the DB as one user. What I ended up having to do was make an unsafe assembly that calls a command line function WMIC. Yucky solution, but it got the job done. Thanks again for the advice. :)

A: 

are you using SQL Authentication? if yes i think it is not possible, because windows login is not associated with server login.

Andrey
Yes, SQL Authentication. I thought that it might be impossible from SQL, I was hoping somehow that I could do it from C# in that case. It seems that given the IP address, workstation name, etc, there should be a way to find out who is currently logged in at that location.
Brandi
look, it is possible to get user login this information have to be passed to sql server. when it is sql auth mode it is NOT passed. you can get IP and other network stuff but you can't get what is not passed.
Andrey
I understand - it is good to know that SQL is not the route to take in obtaining this information. Are you also saying that this information could not be gotten independently of SQL by means of C#? I guess, what I'm looking for is some C# function to the effect of: given mydomainname, workstationname, ip address, tell me who is currently logged in at this location? From what I can tell so far, something like this doesn't exist, but it's good to have confirmed one way or the other. Thanks for letting me know SQL is a dead end.
Brandi
+1  A: 

You have to pass in the user name from client code as a parameter. We do that is every call from our client code (web sites and services).

However, even then it's not available in a trigger, so I use CONTEXT_INFO.

No magic, no silver bullet I'm sorry.

Your one other option is to look at Security Account Delegation/Passthrough. Google search... there is a lot of good stuff, probably better than one specific link.

If this is not possible, and you say you can't change client code, then it can't be done. The data is simply not available: and deliberately so by the application architecture.

gbn