简体   繁体   English

从SID获取user @ domain而不是domain \\ user

[英]From SID get user@domain not domain\user

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. 我遇到一个问题,一个长的Windows 2003+名称,其中两个是不同的,因为域\\用户长度限制,因为新的表单没有限制。

I'm under C#, and while I can do the following: 我在C#下,虽然我可以做以下事情:

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. 这并不总是正确的,正如我在我的介绍中所说,用户名@ domain与用户名的旧Windows NT形式不一样。 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. 如果您不相信我,请进入2k3 +盒子上的AD用户和计算机,看看旧NT用户名与新用户名的区别。

So how do I guarantee I get the right username@domain from a SID? 那么我如何保证从SID获得正确的用户名@ domain? Add to that, I also need this type of thing to work for local users/groups. 除此之外,我还需要这种类型的东西为本地用户/组工作。

The Windows API to get this is called DsCrackNames - http://msdn.microsoft.com/en-us/library/ms675970 . 用于获取此功能的Windows API称为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. 它将根据您提供的标志以任意数量的格式提供输出。

Can't you use System.DirectoryServices.AccountManagement.Principal and the UPN (your name@domain.com) to look up the Sid (also a property on the principal)? 你不能使用System.DirectoryServices.AccountManagement.Principal和UPN(你的name@domain.com)来查找Sid(也是主体上的属性)?
http://msdn.microsoft.com/en-us/library/bb340707.aspx 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 这是一个TechNet代码段,它使用DirectorySearcher通过UPN搜索用户
http://gallery.technet.microsoft.com/ScriptCenter/de2cb677-f930-40a5-867d-ea0326ccbcdb/ http://gallery.technet.microsoft.com/ScriptCenter/de2cb677-f930-40a5-867d-ea0326ccbcdb/

After fetching the principal you should be able to get the Sid property. 获取主体后,您应该能够获得Sid属性。

I have post some C# code for retreiving user data from SID , here is the same aapted to your question : 我发布了一些用于从SID中检索用户数据的 C#代码,这与您的问题相同:

/* Retreiving object from SID 
  */ 
string SidLDAPURLForm = "LDAP://WM2008R2ENT:389/<SID={0}>"; 
System.Security.Principal.SecurityIdentifier sidToFind = new System.Security.Principal.SecurityIdentifier("S-1-5-21-3115856885-816991240-3296679909-1106"); 

DirectoryEntry userEntry = new DirectoryEntry(string.Format(SidLDAPURLForm, sidToFind.Value)); 
string name = userEntry.Properties["userPrincipalName"].Value.ToString(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM