Erweiterte Userdaten aus dem Active Directory lesen

27. Juni 2016 10:56

Da wir Entwickler in der Regel bequem sind, war ich auf der Suche nach einer Lösung erweiterte Daten aus dem AD aus zu lesen.
Leider hab ich nur aussagen gelesen "Lade dir doch eine CSV Datei".
Aus diesem Grund hab ich einmal eine Lösung entworfen und würde Sie auch mit euch teilen :)

Code:
OBJECT Codeunit 50098 ReceiveAdditionalADData
{
  OBJECT-PROPERTIES
  {
    Date=27.06.16;
    Time=10:44:37;
    Modified=Yes;
    Version List=Ted;
  }
  PROPERTIES
  {
    Permissions=TableData 2000000120=rimd;
    OnRun=VAR
            User@1000000000 : Record 2000000120;
          BEGIN
            User.SETRANGE("User Name", USERID);
            IF User.FINDFIRST THEN BEGIN
              MESSAGE('update');
              GetActiveDircetoryUserInfo(User);

              //This will only works if you have super rights
              User.MODIFY(TRUE);
            END;
          END;

  }
  CODE
  {

    PROCEDURE GetActiveDircetoryUserInfo@1000000003(VAR User@1000000000 : Record 2000000120);
    VAR
      ADFilter@1000000003 : Text;
      DirectorySearcher@1000000006 : DotNet "'System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.System.DirectoryServices.DirectorySearcher";
      DirectoryEntry@1000000005 : DotNet "'System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.System.DirectoryServices.DirectoryEntry";
      SearchResult@1000000004 : DotNet "'System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.System.DirectoryServices.SearchResult";
      PropertyCollection@1000000002 : DotNet "'System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.System.DirectoryServices.PropertyCollection";
      PropertyValueCollection@1000000001 : DotNet "'System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.System.DirectoryServices.PropertyValueCollection";
    BEGIN
      ADFilter := '(&(objectclass=user)(objectSid=' + User."Windows Security ID" + '))';

      //If u need a specific user to access ad
      //DirectoryEntry := DirectoryEntry.DirectoryEntry(path, username, password);
      //DirectorySearcher := DirectorySearcher.DirectorySearcher(DirectoryEntry, ADFilter);

      DirectorySearcher := DirectorySearcher.DirectorySearcher(ADFilter);
      SearchResult := DirectorySearcher.FindOne;
      DirectoryEntry := SearchResult.GetDirectoryEntry;
      PropertyCollection := DirectoryEntry.Properties;
     
     
      PropertyValueCollection := PropertyCollection.Item('name');
      User."Full Name" := PropertyValueCollection.Value;

      PropertyValueCollection := PropertyCollection.Item('mail');
      User."Authentication Email" := PropertyValueCollection.Value;

      // ...
     

    END;

    BEGIN
    END.
  }
}