【问题标题】:System.DirectoryServices.AccountManagement in .net 2.0.net 2.0 中的 System.DirectoryServices.AccountManagement
【发布时间】:2009-05-29 08:03:46
【问题描述】:

有没有:

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

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

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

【问题讨论】:

    标签: .net asp.net active-directory directoryservices


    【解决方案1】:

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

    马克

    【讨论】:

    • 我使用 Request.ServerVariables["REMOTE_USER"] 而不是 WindowsIdentity.GetCurrent().Name;寿....但是谢谢...工作得很好
    • 小心并确保您处置您的目录条目。他们往往会永远闲逛,一旦你用完并发连接就会卡住。
    • SillyMonkey:好点子 - 这也不意味着是“生产级”代码 - 只是一个插图 sn-p。
    猜你喜欢
    • 2012-09-18
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 2018-06-21
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多