How to Obtain Domain User Name when connecting via SQL Authentication

拥有回忆 提交于 2020-02-04 05:41:09

问题


I have an application which connects to SQL Server 2000 (using a generic SQL Login and SQL Authentication). I would like to implement some logging via triggers to track data changes. I can't use USER_NAME() because that returns the generic account.

I've poked through master..sysprocesses and it doesn't seem to record the username (although it does record the machine name).

Any ideas how, within SQL, to gain access to the username?

(Note: yes, I could pass it in as a variable via the application ... but that would mean I'd have to roll out a new version of the app; I'm trying to do this from within SQL, so I can avoid that, if possible.)


回答1:


You can't. Simple. SQL Server has no knowledge of the end user with a SQL login at all

The same applies if you use a proxy (web server etc) too: you don't know the end user. We use parameters/stored procs to pass in the username from the web server.

I would suggest that you set CONTEXT_INFO in the client which persists for that connection.




回答2:


Have you tried SYSTEM_USER?

SELECT SYSTEM_USER


来源:https://stackoverflow.com/questions/3891441/how-to-obtain-domain-user-name-when-connecting-via-sql-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!