开发者

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 no开发者_JS百科w 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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜