简体   繁体   中英

Can't retrieve EmployeeId from Active Directory

My GetActiveDirectory() method is used to get data from Active Directory using the SamAccountName , and it's working but the problem is the user.EmployeeId return no sign of data.

Why I can't receive the EmployeeId and how can I fix it?

This is my codes:

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

THIS IS A TEMPORARY ANSWER TO UserPrincipal.EmployeeId

I don't know why UserPrincipal.EmployeeId is not working so I decide to use the old way method.

What I've tried to solve my own problem in .EmployeeId is to go back using System.DirectoryServices

Here is my method to get EmployeeId using 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();
                    }
                }
            }
        }

and to complete my project I use 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 is require to my project so I need to use it.

SORRY FOR MY GRAMMAR.

Here is my full code.

No snippet format???

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM