无法从Active Directory检索EmployeeId

时间:2014-09-13 04:50:06

标签: c# asp.net visual-studio-2010 active-directory

我的GetActiveDirectory()方法用于使用SamAccountName从Active Directory获取数据,但它的工作正常,但问题是user.EmployeeId没有返回数据的迹象。< / p>

为什么我无法收到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;
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

这是暂时的答案 UserPrincipal.EmployeeId

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

我在.EmployeeId尝试解决自己的问题的方法是使用System.DirectoryServices

返回

以下是使用EmployeeId

获取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);
        }

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