4

有没有:

字符串名称 = System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName;

.net 2.0 框架中的等价物?它使用 System.DirectoryServices.AccountManagement (ver 3.5) 参考。我尝试在 .net 2.0 框架上使用该文件,但无济于事。

基本上,我想检索 Windows 用户的完整用户名(名字和姓氏)(不是 Request.ServerVariables["REMOTE_USER"],它只提供 Windows 用户名)

4

1 回答 1

7

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 - 无法改变:-(

马克

于 2009-05-29T08:57:37.303 回答