2014-09-13 75 views
0

我的GetActiveDirectory()方法用於使用SamAccountName從Active Directory獲取數據,並且它正在工作,但問題是user.EmployeeId沒有返回任何數據符號。無法從Active Directory檢索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; 
       } 
      } 
     } 
    } 
+0

的'group.GetMembers()'已經返回用戶 - 你爲什麼叫'.FindByIdentity()'每個用戶發現再次檢索用戶對象?對我來說沒有任何意義...... – 2014-09-13 07:46:51

+0

@marc_s第一個.FIndByIdentity()用於「Domain Users」組用戶組,第二個用於在「Domain Users」中獲取一個用戶,組。 – 2014-09-15 23:23:07

+0

是的,我看到了 - 但第二個是**毫無意義** - group.GetMembers()**已經**返回UserPrincipal成員....您應該只使用這些 - 而不要去獲取用戶*再次* .... – 2014-09-16 04:40:24

回答

1

這是一個臨時的答案UserPrincipal.EmployeeId

我不知道爲什麼UserPrincipal.EmployeeId沒有工作,所以我決定使用舊的方式方法。

我一直試圖解決.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

這裏是我的方法來獲得EmployeeId並完成項目中,我使用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需要我的項目所以我需要使用它。

SORRY FOR MY GRAMMAR。

這是我的完整代碼。

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