Sladescross's Blog

Blogging about Sharepoint related stuff

SQL User Name December 14, 2009

Filed under: SQL,SQL Server,Users — sladescross @ 7:21 pm

You’ll have to do a little bit of song and dance in your .NET app, but not too much. To get the name of the current Windows user, the following expression will suffice (tested in C#, but should work in VB):

System.Security.Principal.WindowsIdentity.GetCurrent().Name

You can include that in your connection string, appended to the Application Name or Workstation ID, and then access the value of these connection properties via the built-in T-SQL functions HOST_NAME() and APP_NAME(). This way, you can get these values transparently where necessary for your audit code without having to explicitly pass the user name as a parameter to every single stored procedure call.

Though truthfully, I’d recommend simply using Windows authentication in SQL Server, and getting the user’s login name via SUSER_SNAME().

About these ads
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 63 other followers