我使用的是SharePoint 2010,在我们的生产环境中,我似乎无法得到这些代码来返回任何东西。服务器是为基于索赔的身份验证设置的。
private string GetADName(string userID)
{
try
{
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// define a "query-by-example" principal - here, we search for a UserPrincipal
// and with the first name (GivenName) of "Bruce" and a last name (Surname) of "Miller"
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.SamAccountName = userID;
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
// find all matches
foreach (var found in srch.FindAll())
{
return found.Name;
}
}
catch (Exception ex)
{
this.lblErrors.Text = ex.Message + "<br />\r\n" + ex.StackTrace;
}
return "";
}发布于 2013-10-24 00:21:26
我不得不使用HostingEnvironment.Impersonate()
private string GetADName(string userID)
{
try
{
using (HostingEnvironment.Impersonate())
{
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.SamAccountName = userID.ToLower();
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
foreach (var found in srch.FindAll())
{
if (found.SamAccountName.ToLower() == userID.ToLower())
{
return found.Name;
}
}
}
}
catch (Exception ex)
{
}
return "";
}https://stackoverflow.com/questions/19439873
复制相似问题