2011-08-09 48 views
3

ADでユーザーアカウントのロックを解除するプロパティを設定しようとしていますが、次のコードを使用しています。問題はdeuserAccountControlが含まれておらず、コードが失敗することです。ユーザーアカウントのロック解除

DirectorySearcherを使用してuserAccountControlの値を取得できますが、これはdeを使用してプロパティを設定する際に役立ちません。誰でも私を助けてくれますか?事前

String m_Path = "LDAP://" + distinguishedName; 

using (DirectoryEntry de = new DirectoryEntry(m_Path)) 
{ 
    if (de.Contains("userAccountControl") 
    { 
     int m_Val = (int)de.Properties["userAccountControl"][0].Value; 
     de.Properties["userAccountControl"].Value = m_Val | 0x0001 
     de.CommitChanges; 
    } 
} 

答えて

5

のおかげで、私はあなたがde.PropertiesuserAccountControlの値が含まれているかどうかを確認する必要があると思うだろう!

string ldapPath = "LDAP://" + distinguishedName; 

using(DirectoryEntry de = new DirectoryEntry(ldapPath)) 
{ 
    // check to see if we have "userAccountControl" in the **properties** of de 
    if(de.Properties.Contains("userAccountControl") 
    { 
     int m_Val = (int)de.Properties["userAccountControl"][0].Value ; 
     de.Properties["userAccountControl"].Value = m_Val | 0x0001; 

     de.CommitChanges(); 
    } 
} 

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

を基本的に、あなたはドメインコンテキストを定義し、簡単にADのユーザーおよび/またはグループを検索して操作することができます。

// set up domain context 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 

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

if(user != null) 
{ 
    // unlock user 
    user.UnlockAccount(); 
} 

新しいDS.AMは、ADのユーザーやグループと一緒に遊ぶのがとても簡単です!

関連する問題