可以';t从Active Directory中检索EmployeeId

本文关键字:Directory 检索 EmployeeId Active 可以 | 更新日期: 2023-09-27 18:21:46

My GetActiveDirectory()方法用于使用SamAccountName从Active Directory获取数据,它正在工作,但问题是user.EmployeeId没有返回任何数据。

为什么我无法接收EmployeeId,如何修复?

这是我的代码:

public void GetActiveDirectory(DataTable DataStorage, string SamAccountName)
{
        var domainContext = new PrincipalContext(
           ContextType.Domain, null, _ldapPath, _ldapUsername, _ldapPassword);
        var group = GroupPrincipal.FindByIdentity(domainContext, "Domain Users");
        if (group != null)
        {
            DataStorage.Columns.Add("SamAccountName");
            DataStorage.Columns.Add("Surname");
            DataStorage.Columns.Add("Guid");
            DataStorage.Columns.Add("Enabled");
            DataStorage.Columns.Add("GivenName");
            DataStorage.Columns.Add("EmailAddress");
            DataStorage.Columns.Add("SID");
            DataStorage.Columns.Add("DateCreated");
            DataStorage.Columns.Add("DateModified");
            DataStorage.Columns.Add("EmployeeNumber");
            DataStorage.AcceptChanges();
            foreach (var p in group.GetMembers(false))
            {
                if(p.SamAccountName != null)
                {
                    try
                    {
                        var user = UserPrincipal.FindByIdentity(
                            domainContext, IdentityType.SamAccountName, SamAccountName);
                        if (user != null)
                        {
                            var userDE = (DirectoryEntry)p.GetUnderlyingObject();
                            DateTime dateCreated = userDE.Properties["WhenCreated"].Value != null
                                ? (DateTime)userDE.Properties["WhenCreated"].Value 
                                : DateTime.MinValue;
                            DateTime dateModified = userDE.Properties["WhenChanged"].Value != null
                                ? (DateTime)userDE.Properties["WhenChanged"].Value 
                                : DateTime.MinValue;
                            DataRow dr = DataStorage.NewRow();
                            dr["SamAccountName"] = user.SamAccountName;
                            dr["Surname"] = user.Surname;
                            dr["Guid"] = user.Guid.ToString();
                            dr["Enabled"] = user.Enabled;
                            dr["GivenName"] = user.GivenName;
                            dr["EmailAddress"] = user.EmailAddress;
                            dr["SID"] = user.Sid.Value;
                            dr["EmployeeNumber"] = user.EmployeeId; //Always give an empty space or null.
                            dr["DateCreated"] = dateCreated;
                            dr["DateModified"] = dateModified;
                            DataStorage.Rows.Add(dr);
                            return;
                        }
                    }
                    catch { }
                    break;
                }
            }
        }
    }

可以';t从Active Directory中检索EmployeeId

这是对UserPrincipal.EmployeeId 的临时答复

我不知道为什么UserPrincipal.EmployeeId不起作用,所以我决定使用旧方法。

我试图在.EmployeeId中解决我自己的问题的是使用System.DirectoryServices

以下是我使用System.DirectoryServices 获取EmployeeId的方法

        var oDirecotyrEntry = new DirectoryEntry(
            _ldapPath, _ldapUsername, _ldapPassword, AuthenticationTypes.Secure);
        SearchResultCollection odrSearchResultCollection;
        var odrUser = new DirectoryEntry();
        var odrDirectorySearcher = new DirectorySearcher
        {Filter = "sAMAccountName="+SamAccountName+"", SearchRoot = oDirecotyrEntry};
        using(odrDirectorySearcher)
        {
            odrSearchResultCollection = odrDirectorySearcher.FindAll();
            if(odrSearchResultCollection.Count > 0)
            {
                foreach(SearchResult result in odrSearchResultCollection)
                {
                    var num = result.Properties["employeeNumber"];
                    foreach(var no in num)
                    {
                        dr["EmployeeNumber"] = no.ToString();
                    }
                }
            }
        }

为了完成我的项目,我使用System.DirectoryServices.AccountManagement

var oPricipalContext = new PrincipalContext(
           ContextType.Domain, _ldapPath2, _ldapUsername, _ldapPassword);
        UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPricipalContext, SamAccountName);
        if (oUserPrincipal != null)
        {
            var oDateTime = (DirectoryEntry)oUserPrincipal.GetUnderlyingObject();
            DateTime dateCreated = oDateTime.Properties["WhenCreated"].Value != null
                ? (DateTime)oDateTime.Properties["WhenCreated"].Value
                : DateTime.MinValue;
            DateTime dateChanged = oDateTime.Properties["WhenChanged"].Value != null
                ? (DateTime)oDateTime.Properties["WhenChanged"].Value
                : DateTime.MinValue;
            dr["SamAccountName"] = oUserPrincipal.SamAccountName;
            dr["Surname"] = oUserPrincipal.Surname;
            dr["Guid"] = oUserPrincipal.Guid.ToString();
            dr["Enabled"] = oUserPrincipal.Enabled;
            dr["GivenName"] = oUserPrincipal.GivenName;
            dr["EmailAddress"] = oUserPrincipal.EmailAddress;
            dr["SID"] = oUserPrincipal.Sid.Value;
            dr["DateCreated"] = dateCreated;
            dr["DateModified"] = dateChanged;
            DataStorage.Rows.Add(dr);
        }

System.DirectoryServices.AccountManagement是我的项目所必需的,所以我需要使用它。

对不起我的奶奶

这是我的完整代码。

没有代码段格式???

 using System.DirectoryServices;
 using System.DirectoryServices.AccountManagement;
 public void GetUsers(DataTable DataStorage, string SamAccountName)
    {   
        DataStorage.Columns.Add("SamAccountName");
        DataStorage.Columns.Add("Surname");
        DataStorage.Columns.Add("Guid");
        DataStorage.Columns.Add("Enabled");
        DataStorage.Columns.Add("GivenName");
        DataStorage.Columns.Add("EmailAddress");
        DataStorage.Columns.Add("SID");
        DataStorage.Columns.Add("DateCreated");
        DataStorage.Columns.Add("DateModified");
        DataStorage.Columns.Add("EmployeeNumber");
        DataStorage.AcceptChanges();
        DataRow dr = DataStorage.NewRow();
        //System.DirectoryServices
        var oDirecotyrEntry = new DirectoryEntry(
            _ldapPath, _ldapUsername, _ldapPassword, AuthenticationTypes.Secure);
        SearchResultCollection odrSearchResultCollection;
        var odrUser = new DirectoryEntry();
        var odrDirectorySearcher = new DirectorySearcher
        {Filter = "sAMAccountName="+SamAccountName+"", SearchRoot = oDirecotyrEntry};
        using(odrDirectorySearcher)
        {
            odrSearchResultCollection = odrDirectorySearcher.FindAll();
            if(odrSearchResultCollection.Count > 0)
            {
                foreach(SearchResult result in odrSearchResultCollection)
                {
                    var num = result.Properties["employeeNumber"];
                    foreach(var no in num)
                    {
                        dr["EmployeeNumber"] = no.ToString();
                    }
                }
            }
        }
        //System.DirectoryServices.AccountManagement
        var oPricipalContext = new PrincipalContext(
           ContextType.Domain, _ldapPath2, _ldapUsername, _ldapPassword);
        UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPricipalContext, SamAccountName);
        if (oUserPrincipal != null)
        {
            var oDateTime = (DirectoryEntry)oUserPrincipal.GetUnderlyingObject();
            DateTime dateCreated = oDateTime.Properties["WhenCreated"].Value != null
                ? (DateTime)oDateTime.Properties["WhenCreated"].Value
                : DateTime.MinValue;
            DateTime dateChanged = oDateTime.Properties["WhenChanged"].Value != null
                ? (DateTime)oDateTime.Properties["WhenChanged"].Value
                : DateTime.MinValue;
            dr["SamAccountName"] = oUserPrincipal.SamAccountName;
            dr["Surname"] = oUserPrincipal.Surname;
            dr["Guid"] = oUserPrincipal.Guid.ToString();
            dr["Enabled"] = oUserPrincipal.Enabled;
            dr["GivenName"] = oUserPrincipal.GivenName;
            dr["EmailAddress"] = oUserPrincipal.EmailAddress;
            dr["SID"] = oUserPrincipal.Sid.Value;
            dr["DateCreated"] = dateCreated;
            dr["DateModified"] = dateChanged;
            DataStorage.Rows.Add(dr);
        }
    }