How to clone a UserPrincipal object in VB.NET

泪湿孤枕 提交于 2020-08-10 18:54:53

问题


I'm trying to create a new User in Active Directory from a VB.NET application. Most fields will be identical to an already existing "template" user, except things like Name, SurName, Email, SamAccountName, etc.

So I want to copy or clone this template User, assign the few fields with a new/different value and then save this new user in Active Directory. I'd like to avoid having to manually assign who-knows how many properties from my template to the new User and maybe forget something along the way..

Is there not a way to do that? I found something about using DirectoryEntry.CopyTo(), but I simply get a "Not implemented" error, and anyway I doubt this is the right direction (Unsure how to use this class together with a UserPrincipal object)

Should I be using a different class than System.DirectoryServices.AccountManagement.UserPrincipal to save my new user in the AD? Do I have the wrong approach?

Any help will be appreciated :)


回答1:


If you look at the source code for DirectoryEntry.CopyTo, it calls:

newParent.ContainerObject.CopyHere(Path, newName)

And ContainerObject is of the type IADsContainer. If you look at the documentation for IADsContainer.CopyHere, it says (under Remarks):

The providers supplied with ADSI return the E_NOTIMPL error message.

ADSI is "Active Directory Service Interfaces". So the short answer is that it just won't work with AD objects.

There is no way to do what you want. You will have to manually assign each attribute you want to copy.



来源:https://stackoverflow.com/questions/62898535/how-to-clone-a-userprincipal-object-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!