C# 获取当前用户的 SID 的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1214022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the best way to get the current user's SID?
提问by Max Schmeling
Prerequisite Detail
先决条件详细信息
- Working in .NET 2.0.
- The code is in a common library that could be called from ASP.Net, Windows Forms, or a Console application.
- Running in a Windows Domain on a corporate network.
- 在 .NET 2.0 中工作。
- 代码位于可以从 ASP.Net、Windows 窗体或控制台应用程序调用的公共库中。
- 在公司网络上的 Windows 域中运行。
The Question
问题
What is the best way to get the current user's SID? I'm not talking about the identity that is executing the application, but the user who is accessing the interface. In background applications and desktop based applications this should be the identity actually executing the application, but in ASP.Net (without impersionation) this should be the HttpContext.Current.User SID.
获取当前用户的 SID 的最佳方法是什么?我不是在谈论正在执行应用程序的身份,而是正在访问界面的用户。在后台应用程序和基于桌面的应用程序中,这应该是实际执行应用程序的标识,但在 ASP.Net(没有模拟)中,这应该是 HttpContext.Current.User SID。
The Current Method
当前方法
This is what I've got right now. It just seems... wrong. It's nasty. Is there a better way to do this, or some built in class that does it for you?
这就是我现在所拥有的。只是好像……错了。真恶心。有没有更好的方法来做到这一点,或者一些内置的类可以为您做到这一点?
public static SecurityIdentifier SID
{
get
{
WindowsIdentity identity = null;
if (HttpContext.Current == null)
{
identity = WindowsIdentity.GetCurrent();
}
else
{
identity = HttpContext.Current.User.Identity as WindowsIdentity;
}
return identity.User;
}
}
采纳答案by Wyatt Barnett
I don't think there is a better way at getting at this info--you've got to get at the WindowsPrincipal somehow and .NET's rather clean API abstracts that behind the User object. I'd just leave this nice and wrapped up in a method and call it a day.
我不认为有更好的方法来获取这些信息——你必须以某种方式获取 WindowsPrincipal 和 .NET 相当干净的 API 抽象,它隐藏在 User 对象后面。我只是留下这个好东西,并用一种方法包装起来,然后收工。
Well, ok, there is one thing you should do (unless your web users are alwaysgoing to be WindowsIdentity), which would be to check for null identity and handle it according to whatever rules you have.
好吧,您应该做一件事(除非您的 Web 用户始终是 WindowsIdentity),那就是检查空身份并根据您拥有的任何规则进行处理。
回答by Joel Etherton
The WindowsIdentity class is the "built in class that does it for you". You've got as simple a solution as you're going to get really, as long as you have a valid WindowsIdentity to work with.
WindowsIdentity 类是“为您完成它的内置类”。只要您有一个有效的 WindowsIdentity 可以使用,您就可以获得与您真正想要的一样简单的解决方案。
Alternatively, if you have the username of the user in question and you want to get the SID directly from AD, you can build your own library to use the DirectoryServices namespace and retrieve the DirectoryEntry for your user (this is a fairly complicated process as DirectoryServices is tricky). You can even use LDAP to get it if you have a need.
或者,如果您有相关用户的用户名并且想要直接从 AD 获取 SID,您可以构建自己的库来使用 DirectoryServices 命名空间并为您的用户检索 DirectoryEntry(这是一个相当复杂的过程,因为 DirectoryServices很棘手)。如果需要,您甚至可以使用 LDAP 来获取它。
回答by Garric
Without the use of third-party libraries This code will give correct results, if the user changed his user name.
不使用第三方库 如果用户更改了用户名,此代码将给出正确的结果。
String SID = "";
string currentUserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
RegistryKey regDir = Registry.LocalMachine;
using (RegistryKey regKey = regDir.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\SessionData", true))
{
if (regKey != null)
{
string[] valueNames = regKey.GetSubKeyNames();
for (int i = 0; i < valueNames.Length; i++)
{
using (RegistryKey key = regKey.OpenSubKey(valueNames[i], true))
{
string[] names = key.GetValueNames();
for (int e = 0; e < names.Length; e++)
{
if (names[e] == "LoggedOnSAMUser")
{
if (key.GetValue(names[e]).ToString() == currentUserName)
SID = key.GetValue("LoggedOnUserSID").ToString();
}
}
}
}
}
}MessageBox.Show(SID);