从 VB.NET 创建新用户时,从异常详细信息中获取 Active Directory 重复服务属性

Get the Active Directory repeated service attribute from the exception details when creating a new user from VB.NET

提问人:Roger 提问时间:10/27/2022 最后编辑:marc_sRoger 更新时间:11/3/2022 访问量:62

问:

我正在使用在 Active Directory 中创建新用户的现有 vb.net 代码。代码正常工作,但在某些未知情况下会抛出错误

指定的目录服务属性或值已存在

如何猜测哪个服务属性已经存在,查看抛出的异常?

我试图捕获异常详细信息,内部异常详细信息(例如。ToString + ex.Message.xxx 等)、stacktrace、异常。数据。。。但我无法猜到这种程度的细节。

代码如下:

Try
Dim dirEntry As DirectoryEntry = GetDirectoryEntry(myLocation)
Dim newUser As DirectoryEntry = dirEntry.Children.Add("cn=" & surnames + "\, " & name, "user")
newUser.Properties("displayName").Add(surnames & ", " & name)
newUser.Properties("name").Add(surnames & ", " & name)
newUser.Properties("sn").Add(surnames)
newUser.Properties("givenName").Add(name)
newUser.Properties("samAccountName").Add(userName)
newUser.Properties("userPrincipalName").Add(userName & domainAccountMail)
newUser.Properties("mailNickname").Add(userName)
newUser.Properties("mobile").Add(myTelephone)
newUser.Properties("mail").Add(myMail)
newUser.Properties("targetAddress").Add("SMTP:" & userName & CDomainMailCloudGeneral)
newUser.Properties("proxyAddresses").AddRange(New Object() {"SMTP:" & myMail, "smtp:" & userName & domainAcountMail, "smtp:" & userName & CDomainMailNubeGeneral})
newUser.Properties("userAccountControl").Value = &H2 Or &H200
newUser.CommitChanges()
Catch ex As Exception

当我写这条消息时,我认为 catch 应该更具体地针对异常类型,但我不知道是哪一个;我应该记录吗?GetType() 中。ToString 来获取它,然后使用特定的异常类型更改捕获,因此,以这种方式获取更多详细信息?它能起作用吗?

提前致谢,

知道了

.NET vb.net 异常 active-directory

评论

1赞 Gabriel Luci 10/27/2022
该 OU 中是否已存在另一个同名帐户?
0赞 Roger 10/28/2022
出于安全原因,我无法直接访问 Windows Active Directory;这是不可能的。正如我在主要问题中提到的,我需要的是一种从异常中提取详细信息的方法。
0赞 Gabriel Luci 10/29/2022
向你展示了什么?ex.ToString()

答:

0赞 Tawab Wakil 11/3/2022 #1

正如你所观察到的,异常可能没有提供足够的详细信息来识别错误的来源。即使有,你也不应该依赖 try/catch 来做到这一点。相反,在每个属性分配之前添加一个检查(这是 C#,但如果您需要将其转换为 VB.NET 请告诉我):

valueAlreadyExists = !string.IsNullOrWhiteSpace(GetAdPropertyValue(newUser, "sn"));

if (valueAlreadyExists)
{
    newUser.Properties["sn"].Value = surname;
}
else  // do an ADD operation
{
    newUser.Properties["sn"].Add(surname);
}

此方法允许您通过在要检查的条件下添加断点或日志语句来查看哪个属性/值已存在。

实现如下:GetAdPropertyValue

private string GetAdPropertyValue(DirectoryEntry userAccount, string adPropertyKey)
{
    string result = null;
    PropertyValueCollection property = userAccount.Properties[adPropertyKey];

    if (property != null)
    {
        object propertyValue = property.Value;

        if (propertyValue != null)
        {
            string propertyValueString = (string)propertyValue;
            result = propertyValueString.Trim();
        }
    }

    return result;
}

当我们在这里时,另一个需要注意的问题是,如果值为空,AD 可能会对它期望 null 与空字符串的位置很挑剔。这可以通过以下方式进行补救:

if (valueAlreadyExists)
{
    if (surname == "")  // assignment below wont accept empty strings, so convert it first
        surname = null;

    newUser.Properties["sn"].Value = surname;
}
else  // do an ADD operation
{
    if (surname == null)  // Add below wont accept nulls, so convert it first
        surname = "";

    newUser.Properties["sn"].Add(surname);
}

您可能希望将此代码包装在方法中,而不是对每个属性重复它。

评论

0赞 Roger 11/4/2022
非常感谢Tawab!下周一我会试一试......似乎是一个很好的解决方法。