C# 应用程序创建的广告用户isn';t正确解析为upn后缀
我正在编写一些以编程方式创建广告用户的代码(将被拉入MS DYnamics CRM 2013环境),该代码有一个奇怪的怪癖。我有一个在我们的广告结构上创建的UPN的列表,但由于某些原因,我的广告用户没有解析它们 所以,我有一个UPN后缀的列表,其中包括example.com。我将用户名设置为第一个。last@example.com,它不允许我使用此登录到CRM。当我检查广告条目时,我可以看到它正确地将登录名分配给了first。last@example.com,但@example.com在列表中出现两次,即实际创建的条目和新条目。所以它没有意识到@example.com是一个预先存在的UPN后缀,我不能使用第一个。last@example.com要使用登录CRM,我必须使用示例.local\first.last。我希望这是有道理的。多谢各位 那么我该如何告诉ThenAdRecord何时登录使用预先存在的UPN而不是。。。不管它在做什么?这是我的密码:C# 应用程序创建的广告用户isn';t正确解析为upn后缀,c#,active-directory,dynamics-crm-2013,C#,Active Directory,Dynamics Crm 2013,我正在编写一些以编程方式创建广告用户的代码(将被拉入MS DYnamics CRM 2013环境),该代码有一个奇怪的怪癖。我有一个在我们的广告结构上创建的UPN的列表,但由于某些原因,我的广告用户没有解析它们 所以,我有一个UPN后缀的列表,其中包括example.com。我将用户名设置为第一个。last@example.com,它不允许我使用此登录到CRM。当我检查广告条目时,我可以看到它正确地将登录名分配给了first。last@example.com,但@example.com在列表中出
try
{
string connectionPrefix = "LDAP://" + ldapPath;// ldapPart;// ldapPath
var adminUsername = ConfigurationHelper.GetConfigSettingByName(orgservice,
"ADPasswordReset.AdminUsername", unsecureConfig, secureConfig);
var adminPassword = ConfigurationHelper.GetConfigSettingByName(orgservice,
"ADPasswordReset.AdminPassword", unsecureConfig, secureConfig);
if (CheckIfUserExists(getSAMNameFromUserName(userName), trace) == true)
{
throw new Exception("A User with that name already exists.");
}
DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix, adminUsername, adminPassword, AuthenticationTypes.Secure);
DirectoryEntry newUser;
string cn = firstName + " " + lastName;
newUser = dirEntry.Children.Add("CN=" + cn, "user"); //display name - This is the "Display" name that shows up on the AD list.
newUser.Properties["displayName"].Value = cn;
newUser.Properties["samAccountName"].Value = getSAMNameFromUserName(userName);//userName;
newUser.Properties["userPrincipalName"].Value = checkUserName(userName);
newUser.Properties["givenName"].Value = firstName; //Firstname
newUser.Properties["sn"].Value = lastName; //Lastname? -Surname
newUser.Properties["LockOutTime"].Value = 0; //unlock account. Set this to 0 to unlock the account.
newUser.CommitChanges();
oGUID = newUser.Guid.ToString();
//Must be handled after the previous stuff. Unsure why.
newUser.Invoke("SetPassword", new object[] { userPassword });
newUser.CommitChanges();
//For some reason, can't be handled before the password is set?
newUser.Properties["userAccountControl"].Value = 0x0200; //0x0200
newUser.CommitChanges();
dirEntry.Close();
newUser.Close();
}
public static string checkUserName(string userName)
{
if (!userName.Contains("@"))
{
return userName + "@example.local";
}
return userName;
}
public static string getSAMNameFromUserName(string domainUserName)
{
int stop;
string s = domainUserName;
if (s.Contains("@"))
{
stop = s.IndexOf("@");
return (stop > -1) ? s.Substring(0, stop) : string.Empty;
}
return domainUserName;// string.Empty;
}
在代码中将UPN设置为
example.local
而不是example.com
:
public static string checkUserName(string userName)
{
if (!userName.Contains("@"))
{
return userName + "@example.local";
}
return userName;
}
即使域配置了多个可能的后缀,用户也只能有一个UPN。如果您想要
username@example.com
要解决此问题,用户必须将example.com
设置为后缀。感谢所有花时间提供帮助的人
空白。Grrr,空白
对于将来遇到这个问题的任何人来说,问题是在广告创建过程中,我的用户名和域中添加了空格。因此,它不是“example.com”,而是将域保存为“example.com”(注意末尾的空格?)。I.Trim()处理了所有内容,看起来工作正常。:)
我的新代码变成:
public static string checkUserName(string userName)
{
if (!userName.Contains("@"))
{
return userName.Trim() + "@domain.local".Trim();
}
return userName.Trim();
}
try
{
string connectionPrefix = "LDAP://" + ldapPath;// ldapPart;// ldapPath
var adminUserName = GetAdminUserName(orgservice, unsecureConfig, secureConfig);
var adminPassword = GetAdminPassword(orgservice, unsecureConfig, secureConfig);
if (CheckIfUserExists(getSAMNameFromUserName(userName), trace) == true)
{
trace.Trace("About to handle success. A User already exists: " + getSAMNameFromUserName(userName));
trace.HandleSuccess();
throw new Exception("User " + getSAMNameFromUserName(userName) + " already exists.");
}
DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix, adminUserName, adminPassword, AuthenticationTypes.Secure);
DirectoryEntry newUser;
string cn = firstName.Trim() + " " + lastName.Trim();
newUser = dirEntry.Children.Add("CN=" + cn, "user"); //display name - This is the "Display" name that shows up on the AD list.
newUser.Properties["displayName"].Value = cn;
newUser.Properties["samAccountName"].Value = getSAMNameFromUserName(userName).Trim();
newUser.Properties["userPrincipalName"].Value = checkUserName(userName).Trim();
newUser.Properties["givenName"].Value = firstName.Trim(); //Firstname
newUser.Properties["sn"].Value = lastName.Trim(); //Lastname? -Surname
//newUser.Properties["LockOutTime"].Value = 0; //unlock account. Set this to 0 to unlock the account.
newUser.CommitChanges();
oGUID = newUser.Guid.ToString();
//Must be handled after the previous stuff. Unsure why.
newUser.Invoke("SetPassword", new object[] { userPassword });
newUser.CommitChanges();
//For some reason, can't be handled before the password is set?
newUser.Properties["userAccountControl"].Value = 0x10200; //0x0200
newUser.CommitChanges();
//http://stackoverflow.com/questions/20710535/is-there-a-way-to-set-a-new-users-domain-suffix-through-the-userprincipal-class
newUser.Close();
dirEntry.Close();
//newUser.Close(); //Close user first, then dirEntry because of the heirarchy call?
}
catch (System.DirectoryServices.DirectoryServicesCOMException E)
{
System.DirectoryServices.DirectoryServicesCOMException newE = new System.DirectoryServices.DirectoryServicesCOMException(E.Message);
//DoSomethingwith --> E.Message.ToString();
throw newE;
}
谢谢你的回复,Ashigore。me@example.com可能是作为用户名传入的内容。如果用户选择first.last而不是电子邮件地址,则会附加example.local。否则,它只返回电子邮件地址,因为我们假设我们已经在AD中设置了域。问题是无法识别域已注册,因此它创建了两次。@JoshAlcorn您不能将用户的电子邮件地址用作用户名,除非它配置为用户的UPN。您好,Ashigore。有问题的用户将使用他们的电子邮件地址作为他们的upn。这些电子邮件地址的域已在我的active directory中注册。通过电子邮件创建广告帐户会将该域两次列入我的列表,并且不允许他们使用自己的电子邮件地址进行物理登录。我希望这是有道理的。到目前为止,我们所有的广告用户都是手工创建的,我使用电子邮件地址注册他们。我只是在程序上有点问题。希望这张照片能比我画的更好@好吧,我想我现在明白了。我建议尝试在不指定UPN的情况下创建用户(即使用默认UPN创建用户),然后再进行更改。看看这是否有区别。谢谢你的建议。它修复了双UPN输入问题,但由于某种原因,我仍然无法使用电子邮件地址登录我们的CRM。然而,当我手工创建广告用户时,电子邮件登录却能如期工作。我现在正在调查,但如果你有任何建议,我很乐意听取。非常感谢你的帮助。