Create Active Directory user in .NET (C#)

前端 未结 3 2008
情深已故
情深已故 2021-02-01 06:08

I need to create a new user in Active Directory. I have found several examples like the following:

using System;
using System.DirectoryServices;

namespace test          


        
3条回答
  •  情书的邮戳
    2021-02-01 06:47

    I think you are calling CommitChanges on the wrong DirectoryEntry. In the MSDN documentation (http://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentries.add.aspx) it states the following (emphasis added by me)

    You must call the CommitChanges method on the new entry to make the creation permanent. When you call this method, you can then set mandatory property values on the new entry. The providers each have different requirements for properties that need to be set before a call to the CommitChanges method is made. If those requirements are not met, the provider might throw an exception. Check with your provider to determine which properties must be set before committing changes.

    So if you change your code to user.CommitChanges() it should work, if you need to set more properties than just the account name then you should get an exception.

    Since you're currently calling CommitChanges() on the OU which hasn't been altered there will be no exceptions.

提交回复
热议问题