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 "";
}