2011-12-03 13 views
0

私はいくつかのLDAP値を取得し、暗号化を使用してデータベースに挿入しようとしています。私は挿入が働いているが、私はユーザーがまだグループの一部であるかどうかをチェックし、DBから削除しないかどうかを確認する必要があります。また、新しいユーザーが追加された場合、既存のユーザーを挿入する代わりに挿入します。あなたは私にこのベストプラクティスの方向性を教えてもらえますか?私は、テーブルを切り捨ててすべてを再挿入しないことをお勧めします。SQLからLDAPユーザーをプログラムで更新および削除する方法はありますか。

 try 
     { 
      /* Connection to Active Directory */ 
      DirectoryEntry deBase = new DirectoryEntry("LDAP://" + txtLDAP.Text + ":" + txtLDapPort.Text + "/" + txtBadeDN.Text, txtUsername.Text, txtPassword.Text, AuthenticationTypes.Secure); 

      /* Directory Search*/ 
      DirectorySearcher dsLookForGrp = new DirectorySearcher(deBase); 
      dsLookForGrp.Filter = String.Format("(cn={0})", txtGroup.Text); 
      dsLookForGrp.SearchScope = SearchScope.Subtree; 
      dsLookForGrp.PropertiesToLoad.Add("distinguishedName"); 
      SearchResult srcGrp = dsLookForGrp.FindOne(); 

      /* Directory Search 
      */ 
      DirectorySearcher dsLookForUsers = new DirectorySearcher(deBase); 
      dsLookForUsers.Filter = String.Format("(&(objectCategory=person)(memberOf={0}))", srcGrp.Properties["distinguishedName"][0]); 
      dsLookForUsers.SearchScope = SearchScope.Subtree; 
      dsLookForUsers.PropertiesToLoad.Add("objectSid"); 
      dsLookForUsers.PropertiesToLoad.Add("sAMAccountName"); 
      SearchResultCollection srcLstUsers = dsLookForUsers.FindAll(); 

      StringBuilder sbUsers = new StringBuilder(); 

      foreach (SearchResult sruser in srcLstUsers) 
      { 
       SecurityIdentifier sid = new SecurityIdentifier((byte[])sruser.Properties["objectSid"][0], 0); 
       string ConnString = "ConnectionString Removed"; 
       string SqlString = "spInsertADAuthorization"; 
       using (OleDbConnection conn = new OleDbConnection(ConnString)) 
       { 
        using (OleDbCommand cmd = new OleDbCommand(SqlString, conn)) 
        { 
         cmd.CommandType = CommandType.StoredProcedure; 
         cmd.Parameters.AddWithValue("AD_Account", SpartaCrypto.SpartaEncryptAES(sruser.Properties["sAMAccountName"][0].ToString(), "thisisasharedsecret")); 
         cmd.Parameters.AddWithValue("AD_SID", SpartaCrypto.SpartaEncryptAES(sid.ToString(), "thisisasharedsecret")); 
         cmd.Parameters.AddWithValue("AD_EmailAddress", "[email protected]"); 
         cmd.Parameters.AddWithValue("DateImported", DateTime.Now.ToString()); 
         cmd.Parameters.AddWithValue("Active", 1); 
         conn.Open(); 
         cmd.ExecuteNonQuery(); 
         conn.Close(); 
        } 
       } 
       lblResults.Text = srcLstUsers.Count + " Users granted access."; 
      } 
     } 

     catch (Exception ex) 
     { 
      if (ex.Message.Contains("Logon failure")) 
      { 
       lblResults.Text = "Logon Failure. Check your username or password."; 
      } 

      if (ex.Message.Contains("The server is not operational")) 
      { 
       lblResults.Text = "LDAP Error. Check your hostname or port."; 
      } 
      if (ex.Message.Contains("Object reference not set to an instance of an object")) 
      { 
       lblResults.Text = "LDAP Error. Check your hostname, port, or group name and try again."; 
      } 


     } 

答えて

0

は、.NET 3.5とアップにしているので、あなたはSystem.DirectoryServices.AccountManagement(S.DS.AM)名前空間をチェックアウトする必要があります。ここではそれについてのすべてを読む:

あなたの検索を行うことPrincipalSearcherと "例による問合せ" プリンシパルを使用することができます。

// create your domain context 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 

// define a "query-by-example" principal - here, we search for a UserPrincipal 
// and with the first name (GivenName) of "Bruce" 
UserPrincipal qbeUser = new UserPrincipal(ctx); 
qbeUser.GivenName = "Bruce"; 

// create your principal searcher passing in the QBE principal  
PrincipalSearcher srch = new PrincipalSearcher(qbeUser); 

// find all matches 
foreach(var found in srch.FindAll()) 
{ 
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....   
} 

1人のプリンシパルと作業するために、プログラミングインターフェイスもかなり良いです:

// find a user 
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName"); 

if(user != null) 
{ 
    // do something here... you can access most of the commonly used properties easily 
    user.GivenName = "...."; 
    user.Surname = "......"; 
    user.SamAccountName = "....."; 
} 

// find the group in question 
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere"); 

// if found.... 
if (group != null) 
{ 
    // iterate over members 
    foreach (Principal p in group.GetMembers()) 
    { 
     Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName); 
     // do whatever you need to do to those members 
    } 
} 

新しいS.DS.AMを使用すると、実際にと表示され、はADのユーザーとグループで遊ぶことができます。

+0

私はそれを見ていきます、ありがとう。しかし、私がこれから得ようとしているアイデアは、グループに入ったばかりのユーザーの挿入を管理し、既にDBにあるユーザーを再挿入しない方法です。また、DB内にあるが、もはやグループの一部ではないユーザーを削除する。 –

関連する問題