views:

96

answers:

2

Hello

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? :)

Torben

+3  A: 

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;
abatishchev
Hi. Thats not solving my problem, because the web application accesses the database always with the same login. So if three user access the application on the same time, they all use the same login for accessing the database.
Torben H.
Hi. Thanks. Seems like this is the only way ... but this is too much to change :)
Torben H.
@Torben H.: Nice that helped :) Unfortunately this is only what I can to suggest
abatishchev
A: 

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.
devstuff
Actually this won't work, just re-read the question. Use @abatishchev's suggestion to pass the session-based user to the database.
devstuff