如何使用MSSQL数据库在C#中获取当前登录的用户 [英] How to get the currently logged in user in C# with MSSQL database

查看:267
本文介绍了如何使用MSSQL数据库在C#中获取当前登录的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我有一个情况。我需要能够识别谁与底层MSSQL服务器连接到该软件。所以我可以按用户名跟踪谁登录软件。



我尝试了什么:



所以我使用的代码:

 System.Security.Principal.WindowsIdentity.GetCurrent()名称

。但是这返回了数据库用户的用户名,而不是用户登录软件的用户名。

接下来我试过:

 Environment.UserName 

但这会选择登录操作系统的用户的用户名。



想法是当软件启动时,用户必须登录。我需要能够得到这个用户的用户名,也许洛和loggout时间戳。


$ B $供我使用时间戳B'pre> GetTimestamp(DateTime.Now)

解决方案

各种评论所说的是您的C#代码知道谁登录 - 因为它负责登录过程 用于该软件

SQL不知道谁登录到特定的软件,甚至是特定的PC因为它是基于服务器的系统 - 这意味着实际访问数据库并将数据提供给应用程序的代码通常完全在不同的计算机上(并且甚至不作为该系统的登录用户运行,它在专门为MsSql应用程序本身创建的特殊用户帐户下运行。



因此,当您的应用程序启动时,请将您的用户登录并 记住他是哪个用户

然后你可以传递那个inf SQL的序列作为登录/注销时间戳或直接日志用户活动。但SQL无法做到这一点,除非您为登录系统的每个用户在SQL 中创建单独的用户帐户 - 这在系统中可能不是一个好主意有很多用户!


这将为您提供当前数据库用户的名称。如果您使用的是Windows登录,则它将是当前Windows用户的名称。如果您正在使用数据库登录,它将是登录数据库的db用户的名称。



  SELECT   CURRENT_USER ; 


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)

解决方案

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!


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.

SELECT CURRENT_USER;


这篇关于如何使用MSSQL数据库在C#中获取当前登录的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆