Given the following code, why do I get a PrincipalExistsException
when I know for a fact that the principal doesn't exist and isn't actually made?
public UserPrincipal Add(
string givenName,
string surname,
string domain) {
UserPrincipal principal = new UserPrincipal(context: base.Context) {
Enabled = true
};
if (!String.IsNullOrEmpty(givenName) && !String.IsNullOrEmpty(surname) && !String.IsNullOrEmpty(domain)) {
this.RenameInternal(principal: principal, givenName: givenName, surname: surname, domain: domain);
principal.Save();
};
return principal;
}
I can confirm that the RenameInternal()
method works just fine because it get's called by a method named Rename()
. So, there must be an issue with how the object is created and/or saved, but I don't know how to find out where the error is. This seems like simple enough code...
Looking through the domain controller (Windows Server 2008 R2) I can't find the "newly" create principal anywhere, so I'm assuming that it's not being created and that the exception is lying to me somehow.
I'd appreciate any help on this. Thanks in advance.
Ok, I figured it out. The SamAccountName was what was causing the exception to be thrown (thank you Microsoft for the ever helpful error messages). Anyway, it was the issue because it was trying to set it to a name that already exists. I modified it by tossing in a middle initial and it's working fine, or at-least until I get people with the same first initial, middle initial and last name.