2017-11-09 9 views
1

問題をすべてのSidを現在のループに追加できません。他のすべては私が期待したとおりに働いています。私は自分のコードに表示されている各ユーザーのSIDを追加するために自分のコードを追加する手助けが必要です。 SIDが表示されます。C#のActive DirectoryからすべてのユーザーのSIDを追加する

新しいエラーメッセージ:ここに enter image description here

である私の現在のコード:

namespace ActiveDirectoryDisplayNamesApp 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      using (var context = new PrincipalContext(ContextType.Domain, "nor-amcoldcorp.local")) 
      { 
       using (var searcher = new PrincipalSearcher(new UserPrincipal(context))) 
       { 
        foreach (var result in searcher.FindAll()) 
        { 
         DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry; 
         var sidByte = ObjectToByteArray(de.Properties["objectSId"].Value); 
         Console.WriteLine("First Name: " + de.Properties["givenName"].Value); 
         Console.WriteLine("Last Name : " + de.Properties["sn"].Value); 
         Console.WriteLine("SAM account name : " + de.Properties["samAccountName"].Value); 
         Console.WriteLine("User principal name: " + de.Properties["userPrincipalName"].Value); 
         Console.WriteLine("Object Sid: " + System.Text.Encoding.UTF8.GetString(sidByte)); //Here is the changement 
         Console.WriteLine(); 
        } 
       } 
      } 
      Console.ReadLine(); 

     } 

     static public byte[] ObjectToByteArray(Object obj) 
     { 
      if (obj == null) 
       return null; 

      BinaryFormatter bf = new BinaryFormatter(); 
      MemoryStream ms = new MemoryStream(); 
      bf.Serialize(ms, obj); 

      return ms.ToArray(); 
     } 






    } 
} 

答えて

0

de.Properties["objectSid"].valueあなたはFUを取得するには、文字列の中に、これを解析する必要があります。SIDを表示するには、バイト[]配列を返しますあなたが探しているnctionality。それを行う方法に関する良い記事はhereです。

以下

あなたが使用可能な文字列に配列を変換する必要があります関数です。

public static string ConvertByteToStringSid(Byte[] sidBytes) 
{ 
    StringBuilder strSid = new StringBuilder(); 
    strSid.Append("S-"); 
    try 
    { 
     // Add SID revision. 
     strSid.Append(sidBytes[0].ToString()); 
     // Next six bytes are SID authority value. 
     if (sidBytes[6] != 0 || sidBytes[5] != 0) 
     { 
      string strAuth = String.Format 
       ("0x{0:2x}{1:2x}{2:2x}{3:2x}{4:2x}{5:2x}", 
       (Int16)sidBytes[1], 
       (Int16)sidBytes[2], 
       (Int16)sidBytes[3], 
       (Int16)sidBytes[4], 
       (Int16)sidBytes[5], 
       (Int16)sidBytes[6]); 
      strSid.Append("-"); 
      strSid.Append(strAuth); 
     } 
     else 
     { 
      Int64 iVal = (Int32)(sidBytes[1]) + 
       (Int32)(sidBytes[2] << 8) + 
       (Int32)(sidBytes[3] << 16) + 
       (Int32)(sidBytes[4] << 24); 
      strSid.Append("-"); 
      strSid.Append(iVal.ToString()); 

     // Get sub authority count... 
     int iSubCount = Convert.ToInt32(sidBytes[7]); 
     int idxAuth = 0; 
     for (int i = 0; i < iSubCount; i++) 
     { 
      idxAuth = 8 + i * 4; 
      UInt32 iSubAuth = BitConverter.ToUInt32(sidBytes, idxAuth); 
      strSid.Append("-"); 
      strSid.Append(iSubAuth.ToString()); 
     } 
    } 
    catch (Exception ex) 
    { 

    } 
    return strSid.ToString(); 
} 

そして、ここでは、関数を呼び出す必要がありますものです:

System.DirectoryServices.PropertyCollection coll = de.Properties; 
object obVal = coll["objectSid"].Value; 
string yourSID; 
if (null != obVal) 
{ 
    yourSID = ConvertByteToStringSid((Byte[])obVal); 
} 
+0

私のchangementを見てみましょう。 post obove – AndresBryan29

+0

リンクされたポストでメソッドを試しても分かりませんが、試してみましたが、約5分でDirectoryEntryプロパティからSIDが出力されました。 – Laslos

+0

私は結果を使用しているコードを追加します – AndresBryan29

0

EDIT:

宣言この機能:

private byte[] ObjectToByteArray(Object obj) 
    { 
     if(obj == null) 
      return null; 

     BinaryFormatter bf = new BinaryFormatter(); 
     MemoryStream ms = new MemoryStream(); 
     bf.Serialize(ms, obj); 

     return ms.ToArray(); 
    } 

これを行うよりも:

byte[] bytes = Encoding.Default.GetBytes(de.Properties["objectSid"].value); 
sidByte= Encoding.UTF8.GetString(bytes); 

Console.WriteLine("Object Sid: " + sidByte); //Here is the changement 

それとも、(最初のものは動作しなかった場合)、このapprocahを試みるが、あなたのバイトのオブジェクトはバイトに変換する第一の機能を保つ:コールより

関数を宣言し

static string BytesToStringConverted(byte[] bytes) 
{ 
    using (var stream = new MemoryStream(bytes)) 
    { 
     using (var streamReader = new StreamReader(stream)) 
     { 
      return streamReader.ReadToEnd(); 
     } 
    } 
} 

をそれはそのよう:

Console.WriteLine("Object Sid: " +BytesToStringConverted (sidByte) 
+0

こんにちはする@ napi15、私は私はこのsystem.byte []を手に入れました。各ユーザーの実際のSIDは表示されませんでした。 – AndresBryan29

+0

ありがとう私はそれを試して、それがうまくいけば、感謝します、ありがとう。 – AndresBryan29

+0

両方のソリューションでHello @ napi15を追加しました。あなたは何が欠けているのですか? – AndresBryan29

関連する問題