2

.NET System.DirectoryServices 名前空間を使用して、開発中のアクティブ ディレクトリ サーバーに新しいユーザーを作成しようとしています。

次のコードを使用してみます。

DirectoryEntry dirEntry = new DirectoryEntry(path, "TESTDOM\\Administrator", "2109password", AuthenticationTypes.Secure | AuthenticationTypes.ServerBind);

object o = dirEntry.NativeObject;
DirectoryEntry newUser = dirEntry.Children.Add("CN=NewUser4", "user");
newUser.Properties["samAccountName"].Value = "NewUser4";
newUser.Properties["Description"].Add("User Description");

newUser.Invoke("SetPassword",  new object[] {"2109password"} );
newUser.CommitChanges();

私も使用してコミットしようとしました

newUser.CommitChanges();

Invoke を呼び出してパスワードを設定する前に。私は常に TargetInvocationException ラッピングを取得します。

InnerException {"The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)"} System.Exception {System.Runtime.InteropServices.COMException}

例外は常に呼び出したときにのみスローされます

newUser.Invoke("SetPassword",  new object[] {"2109password"} );

SetPassword で Invoke を呼び出す前に newUser.CommitChanges() を呼び出すと、新しいユーザーがドメインに作成されます。その後、手動で AD マシンに移動し、同じパスワードを問題なく設定できます (したがって、パスワード文字列が規則に違反していても問題ありません)。これについてオンラインで多くの投稿があることに気付きましたが、解決策が見つかりませんでした。

コードを実行しているマシンがドメインのメンバーではないという事実と関係があるのではないかと思います。ユーザー TESTDOM\Administrator は、TESTDOM ドメインの管理者、ドメイン管理者、スキーマ管理者、およびエンタープライズ管理者グループのメンバーですが。

.NET 2 を使用しているため、System.DirectoryServices.AccountManagement 名前空間を使用できないことに注意してください。これを解決するために何ができるかについてのアイデアはありますか? 私は必死です

4

3 に答える 3

1

プロパティを設定する前に、まずユーザーを作成する必要があると思います。通常、ユーザーを作成します。

/// <summary>
/// This Method will Create a new User Directory Object based on a Username and LDAP Domain
/// </summary>
/// <param name="sUserName">The Username of the New User</param>
/// <param name="sLDAPDomain">The LDAP Domain for the New User</param>
/// <returns></returns>
public DirectoryEntry CreateNewUser(string sUserName, string sLDAPDomain)
{
    //Set the LDAP qualification so that the user will be Created under the Users Container
    string LDAPDomain = "/CN=Users," + sLDAPDomain;
    oDE = new DirectoryEntry("LDAP://" + sADServer + "/" + sLDAPDomain, sADUser, sADPassword, AuthenticationTypes.Secure);

    oDEC = oDE.Children.Add("CN=" + sUserName, "user");
    oDE.Close();
    return oDEC;
}

次に、必要なプロパティを設定します

/// <summary>
/// This will Set the Property of the Directory Entry Object
/// </summary>
/// <param name="oDE">The Directory Object to Set to</param>
/// <param name="sPropertyName">The Property Name</param>
/// <param name="sPropertyValue">The Property Value</param>
public void SetProperty(DirectoryEntry oDE, string sPropertyName, string sPropertyValue)
{
    //Check if the Value is Valid
    if (sPropertyValue != string.Empty)
    {
        //Check if the Property Exists
        if (oDE.Properties.Contains(sPropertyName))
        {
            oDE.Properties[sPropertyName].Value = sPropertyValue;
            oDE.CommitChanges();
            oDE.Close();
        }
        else
        {
            oDE.Properties[sPropertyName].Add(sPropertyValue);
            oDE.CommitChanges();
            oDE.Close();
        }
    }
}

次にパスワードを設定します

/// <summary>
/// This Method will set the Users Password based on Directory Entry Object
/// </summary>
/// <param name="oDE">The Directory Entry to Set the New Password</param>
/// <param name="sPassword">The New Password</param>
/// <param name="sMessage">Any Messages catched by the Exception</param>
public void SetUserPassword(DirectoryEntry oDE, string sPassword, out string sMessage)
{
    try
    {
        //Set The new Password
        oDE.Invoke("SetPassword", new Object[] { sPassword });
        sMessage = "";

        oDE.CommitChanges();
        oDE.Close();
    }
    catch (Exception ex)
    {
        sMessage = ex.InnerException.Message;
    }

}

そして最後にアカウントを有効にします

/// <summary>
/// This Method will Enable a User Account Based on the Directory Entry Object
/// </summary>
/// <param name="oDE">The Directoy Entry Object of the Account to Enable</param>
public void EnableUserAccount(DirectoryEntry oDE)
{
    oDE.Properties["userAccountControl"][0] = ADMethods.ADAccountOptions.UF_NORMAL_ACCOUNT;
    oDE.CommitChanges();
    oDE.Close();
}

完全な実装については、ここにアクセスできます-> http://anyrest.wordpress.com/2010/02/01/active-directory-objects-and-c/

于 2010-11-02T22:28:14.513 に答える
1

OK、うまくいきました:

 dirEntry = new DirectoryEntry(ldapPath, domainAdminUser, domainAdminPassword);
    dirEntry.Invoke("SetPassword", new object[] { newPassword });
    dirEntry.Properties["LockOutTime"].Value = 0; //unlock account

ldapPath には、変更しようとしているユーザーの完全な DN が含まれている必要があるため、次のようになります。

string ldapPath = "LDAP://ad.domain.com:389/CN=username,OU=Users,DC=ad,DC=domain,DC=com"
于 2010-12-21T16:10:41.153 に答える
0

最初にアカウントを作成してから SetPassword を呼び出す必要があることは正しいです。CommitChanges の代わりに Invoke SetInfo を使用してから SetPassword を試すことができます。

それでもうまくいかない場合は、displayName などの必須プロパティを設定していないと思います。

于 2010-10-17T19:48:46.833 に答える