Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everyone! I have a situation here. I need to be able to identify who connects to the software with an underlying MSSQL server. so i can track who is logged into the software on a per username basis.

What I have tried:

so I used the codes:
System.Security.Principal.WindowsIdentity.GetCurrent().Name
but this returned the username of the database user not thre name of the user logged into the software.
Next I tried:
Environment.UserName
but this picks the username of the user logged into the operating system.

The idea is that when the software is started, a user has to login. I need to be able to get this user's username and perhaps loggin and loggout timestamps.

for the timestamps i use
GetTimestamp(DateTime.Now)
Posted
Updated 31-Jan-17 4:33am
v2
Comments
[no name] 31-Jan-17 8:11am    
"a user has to login", if your software is started, you already know this information. So what is the question again?
Dave Kreskowiak 31-Jan-17 8:43am    
Your question doesn't make much sense. Is this C# code being written in an SQL Server user defined function or stored procedure or trigger or is it only in the application code?

Typically, you use a dedicated user account to access a database, not the user account running the application on the workstation. In this case, to SQL Server, every database access is from the exact same "person".
Nganku Junior 31-Jan-17 8:53am    
the question is how to get this information (username) even after login. So I can identify who did what and when
[no name] 31-Jan-17 9:06am    
Perhaps the comments are unclear to you. The is no information to "get", by your own comments, you already know this information. Since you already know this information there is no reason to get it from anywhere. This is the reason your "question" doesn't make any sense.
Nganku Junior 31-Jan-17 8:56am    
I have created a database for the software to store information in it. User accounts have also been created and stored in some table in the database associated to the software.so these user accounts are the only ones able to authenticate against the login procedure and then access the software. I wish to know how to keep track of who logged in when and perhaps passed what transaction...

What the various comments are saying is that your C# code knows who is logged in - because it is in charge of the login process for that software.
SQL doesn't know "who is logged in" to a particular piece of software, or even to a particular PC because it is a Server based system - which means that the code which actually accesses the database and serves the data back to your application is normally on a different computer altogether (and does not even run as the "logged in user" for that system, it runs under a special user account created specifically for the MsSql application itself).

So when your app starts, log your user in and remember which user he was.
You can then pass that information to SQL either as a "log in"/"log out" timestamp, or a log user activity directly. But SQL can't do that, unless you create a separate user account in SQL for each user you log into your system - which is probably not a good idea in a system with many users!
 
Share this answer
 
Comments
Nganku Junior 1-Feb-17 6:33am    
Thankyou for your down-to-earth explanation. I've got a solution already not very far from what you advice. I actually created three connection strings that I opened just before use and closed immediately after. One handles the login, the other handles the 'user login stamp' and the third handles the 'user logout stamp'.

Thanks again
OriginalGriff 1-Feb-17 6:37am    
You're welcome!
This will give you the current database user's name. If you're using windows login, it will be the name of the current windows user. If you're using database login, it will be the name of the db user that logged into the database.

SQL
SELECT CURRENT_USER;
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900