我需要让特定用户知道“sAMAAccountName”字段。
问题是,这个特定用户可以在许多群体中:
OU=ThirdParty
OU=Company1
CN=User1
CN=User2
CN=User3
OU=Company2
CN=User1
CN=User2
CN=User3
有没有办法让用户不知道他们的群组,只使用他们拥有的一个属性?
我的代码:
DirectorySearcher search = new DirectorySearcher(_path);
search.Filter = "(&(objectCategory=person)(objectClass=User))";
StringBuilder groupNames = new StringBuilder();
try
{
SearchResultCollection result = search.FindAll();
.....
}
谢谢!
修改
好的,我使用这段代码得到它:
DirectorySearcher search = new DirectorySearcher(_entry, "(sAMAccountName=" + userCode + ")");
答案 0 :(得分:1)
您需要了解有关用户的哪些信息?我们过去曾使用过这种类型的代码来检索有关用户的信息
using (var identity = new WindowsIdentity(username))
{
var user = new WindowsPrincipal(identity);
if (user.IsInRole("Some Role Name"))
return true;
return false;
}
修改强> 在您发表评论之后,我想知道这篇文章是否会为您提供更多信息。他们如此表示获取您要求的字段,我只是不确定检索该员工的代码是否适用于您,因为这引用了InfoPath:http://msdn.microsoft.com/en-us/library/bb952744(v=office.12).aspx
答案 1 :(得分:1)
如果您切换到System.DirectoryServices.AccountManagement
,那么您会发现API实际上要简单得多。
例如:
public something FindUserByUserName( string UserName )
{
using ( var searcher =
new PrincipalSearcher( new UserPrincipal( ConfigurationContext ) { Name = UserName } ) )
{
var item = searcher.FindOne();
// do whatever you want with the found object and return it
}
}
其中ConfigurationContext
是一个返回PrincipalContext
的属性(连接到AD的凭据,类似于“连接字符串”)
答案 2 :(得分:0)
试试这个:
public static List<string> GetADUserInfo(string login)
{
//Using Hosting.HostingEnvironment.Impersonate()
List<string> info = new List<string>();
PrincipalContext infPC = new PrincipalContext(ContextType.Domain, "domain", "login", "password");
UserPrincipal infUP = new UserPrincipal(infPC);
PrincipalSearcher infPS = new PrincipalSearcher();
UserPrincipal foundUP;
infUP.SamAccountName = login;
infPS.QueryFilter = infUP;
foundUP = infPS.FindOne();
if (foundUP != null) {
info.Add(foundUP.SamAccountName.ToLower);
info.Add(foundUP.GivenName);
info.Add(foundUP.Surname);
info.Add(foundUP.EmailAddress.ToLower);
return info;
}
return null;
}