【问题标题】:C#: Cannot set the Contact-Attribute "targetAddress" with my DirectoryEntry - ConnectionC#:无法使用我的 DirectoryEntry 设置联系人属性“targetAddress” - 连接
【发布时间】:2024-04-25 23:55:01
【问题描述】:

我想在我们的 AD 中的特定 OU 中创建一个带有一些属性的联系人对象:

sn、givenName、mail、description、displayname、proxyAddresses 和 targetaddress。

我找到了很多示例,如何使用 C# 在 Active Directory 中为联系人对象设置属性,并且我能够创建具有除“targetaddress”之外的所有属性的对象。

有人可以为我指出正确的方向吗?谢谢!

        public void CreateContact2(string Vorname, string Nachname, string EmailAdresse, string Beschreibung, string myDomainPath) 
    {
        string CN = Vorname + " " + Nachname;
        string mailNickName = EmailAdresse.Remove(EmailAdresse.IndexOf("@"));
        string EmailAdresse2 = "SMTP:" + EmailAdresse;
        DirectoryEntry directoryEntry;
        try
        {
            directoryEntry = new DirectoryEntry("LDAP://myDomainPath") 
            directoryEntry.RefreshCache();
            DirectoryEntry contact = directoryEntry.Children.Add("CN=" + CN, "Contact");
            contact.Properties["sn"].Value = Nachname;
            contact.Properties["givenName"].Value = Vorname;
            contact.Properties["mail"].Value = EmailAdresse;
            contact.Properties["description"].Value = Beschreibung;
            contact.Properties["displayName"].Value = Nachname + ", " + Vorname;
            contact.Properties["proxyAddresses"].Add(EmailAdresse2);    
            contact.Properties["targetaddress"].Value = EmailAdresse2;    
            contact.CommitChanges();
        }
        catch (Exception e)
        {
            // Do some error processing
            var msg = e.Message.ToString();
            Console.WriteLine("Fehler in Funktion CreateContact():" + msg);
        }
    }

如果我在没有 targetaddress-Attribute 的情况下创建 Contact-Object 时,它会创建得很好

但有了它我是

获取 System.Runtime.InteropServices.COMException:“指定的 目录服务属性或值不存在”

失败。有什么想法吗?

【问题讨论】:

  • 不区分大小写吧?

标签: c# active-directory contacts


【解决方案1】:

我遇到了同样的问题,我所做的是重新启动并以管理员身份运行 Visual Studio。可能是因为没有足够的权限运行一些COM 方法

【讨论】:

  • @Jayendran:谢谢,我会试试的。
  • 我不这么认为。我几乎尝试了所有方法,包括区分大小写的组合 - 但谢谢!
【解决方案2】:

非常感谢你们! 我自己找到了答案,并且。这是我的错误。该属性未正确委派。

【讨论】:

    最近更新 更多