简体   繁体   中英

SQL Server 2005/2008: Identify current user

I have a web application, which is using a SQL Server 2005 database.

My problem is, that the application has no role management. So the application always accesses the database with one default user. But now I have to save and access a value only for the current user.

Is there any way to do this? Maybe something like a session on the web server? The best way would be, if there is any possibility to access the current session id of the web server from T-SQL.

Do anyone understand my problem? :)

Allows a system-supplied value for the current login to be inserted into a table

DECLARE @sys_usr char(30);
SET @sys_usr = SYSTEM_USER;
SELECT 'The current user is: '+ @sys_usr;
GO

from MSDN

In my opinion, it's better don't do this. Another way: send to stored procedure current user from web sever:

command.CommandText = "EXEC mySP @user";
command.Parameters.Add("@user").Value = ((YourOwnUserClass)Session["user"]).Name;

// or System.Web.HttpContext.Current.User.Identity.Name; to use built-in
// from web page it becomes this.User.Identity.Name;

If you are using Windows integrated authentication instead of SQL accounts:

  1. Give schema object permissions to a Windows group , not a user. Then add all of your application users to this group.
  2. Use the built-in SUSER_NAME() function to retrieve the underlying Windows user name (in loginDomain\\userName format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM