从SID获取user@domain而不是domain\user
这有点晦涩难懂:我需要获取用户/组的 user@domain 形式,但我不需要域\用户形式。我曾经遇到过一个问题,即长 Windows 2003+ 名称,由于域\用户长度限制,两者不相同,因为新表单没有限制。
我使用 C#,虽然我可以执行以下操作:
string GetUserName(SecurityIdentifier SID)
{
NTAccount account = SID.Translate(typeof(NTAccount));
string [] splits = string.Split("\\", account.Value);
return splits[1] + @"@" + splits[0];
}
正如我在介绍中所述,这并不总是正确的,username@domain 不一定与旧的 Windows NT 形式的用户名相同。如果您不相信我,请进入 2k3+ 机器上的 AD 用户和计算机,看看旧 NT 用户名与新用户名的字段有何不同。
那么我如何保证从 SID 获得正确的 username@domain 呢?除此之外,我还需要这种类型的东西为本地用户/组工作。
This is a bit of an obscure one: I need to get the user@domain form of a user/group, but I do NOT want the domain\user form. I encountered a problem once with long windows 2003+ names where the two are NOT the same because of the domain\user length limit, because the new form does not have the limit.
I'm under C#, and while I can do the following:
string GetUserName(SecurityIdentifier SID)
{
NTAccount account = SID.Translate(typeof(NTAccount));
string [] splits = string.Split("\\", account.Value);
return splits[1] + @"@" + splits[0];
}
This isn't always right, as I stated in my intro, the username@domain is NOT NECESSARILY the same as the old windows NT form of the username. If you don't believe me, go into AD Users and computers on a 2k3+ box and see how there's different fields for the old NT username versus the new one.
So how do I guarantee I get the right username@domain from a SID? Add to that, I also need this type of thing to work for local users/groups.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
获取此信息的 Windows API 称为
DsCrackNames
- http:// msdn.microsoft.com/en-us/library/ms675970。根据您提供的标志,它将为您提供任意数量的格式的输出。The Windows API to get this is called
DsCrackNames
- http://msdn.microsoft.com/en-us/library/ms675970. It will give you the output in any number of formats depending on the flags you provide.您不能使用 System.DirectoryServices.AccountManagement.Principal 和 UPN(您的 [email protected] ])查找 Sid(也本金的财产)?
http://msdn.microsoft.com/en-us/library/ bb340707.aspx
以下是使用 DirectorySearcher 通过 UPN 搜索用户的 TechNet 代码段
http://gallery.technet.microsoft.com/ScriptCenter/ de2cb677-f930-40a5-867d-ea0326ccbcdb/
获取后校长你应该能够获得 Sid 财产。
Can't you use System.DirectoryServices.AccountManagement.Principal and the UPN (your [email protected]) to look up the Sid (also a property on the principal)?
http://msdn.microsoft.com/en-us/library/bb340707.aspx
Here is a TechNet snippet that uses a DirectorySearcher to search for a user by UPN
http://gallery.technet.microsoft.com/ScriptCenter/de2cb677-f930-40a5-867d-ea0326ccbcdb/
After fetching the principal you should be able to get the Sid property.
我发布了一些 C# 代码 从SID检索用户数据,这与您的问题相同:
I have post some C# code for retreiving user data from SID, here is the same aapted to your question :