S.DS.AM 命名空间是在 .NET 3.5 中引入的,不幸的是,它没有 2.0 版本。
您可以使用 WindowsIdentity.GetCurrent().Name 查询 ASP.NET 应用程序中的当前 Windows 用户 - 这将为您提供 DOMAIN\UserName。
然后,您必须在 AD 中使用 DirectorySearcher 对象对该用户进行用户搜索,以便找到相应的 DirectoryEntry。这将为您提供该用户的所有信息。
string currentUser = WindowsIdentity.GetCurrent().Name;
string[] domainUserName = currentUser.Split('\\');
string justUserName = domainUserName[1];
DirectoryEntry searchRoot = new DirectoryEntry("LDAP://dc=(yourcompany),dc=com");
DirectorySearcher ds = new DirectorySearcher(searchRoot);
ds.SearchScope = SearchScope.Subtree;
ds.PropertiesToLoad.Add("sn");
ds.PropertiesToLoad.Add("givenName");
ds.Filter = string.Format("(&(objectCategory=person)(samAccountName={0}))", justUserName);
SearchResult sr = ds.FindOne();
if (sr != null)
{
string firstName = sr.Properties["givenName"][0].ToString();
string lastName = sr.Properties["sn"][0].ToString();
}
它有点复杂并且涉及 .NET 2.0 - 无法改变:-(
马克