You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# 获取AD动态通讯组时自定义类型属性未定义报错排查

C# 读取AD动态通讯组代码编译错误修复

两个编译错误的根因及修复方案

  • 错误1:Settings.ImpersonatedUserDomain 未定义
    你误引入了NuGet.Configuration命名空间下的Settings类,该类不存在ImpersonatedUserDomain属性。原片段中的Settings是自定义配置读取逻辑,用来获取当前AD的根域名,不需要依赖NuGet的配置类,直接读取App.config配置项或者硬替换为实际域名即可,同时可以删掉无用的NuGet.Configuration引用,以及Class2中被注释的无效Settings属性。
  • 错误2:dl.DType = new DLT.DDL.set(); 类型不存在
    这里是补全代码时的逻辑误解:一是你在Class2和DistributionList中重复定义了同名DLT结构体,编译器默认优先取当前类(Class2)内的定义,而该结构体下的DDL是List<string>类型的属性,不是嵌套类型,无法通过DLT.DDL的方式调用构造函数;二是不存在.set()方法,原代码此处仅需要给通讯组类型字段赋值,标记当前条目为动态通讯组即可。

修复后可正常编译的完整代码

using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Configuration;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Security.Permissions;
using Microsoft.Win32.SafeHandles;
using System.Runtime.ConstrainedExecution;
using System.Security;
using Outlook = Microsoft.Office.Interop.Outlook; 
using Microsoft.Exchange.WebServices.Data;

/* This code may only be usable over a VPN*/

public class Class2
{
    public List<DistributionList> GetDynamicDistributionLists(string strEmailAddress)
    {
        List<DistributionList> distributionLists = new List<DistributionList>();
        // 替换为你实际的AD根域名,例如域名为corp.com则值为"corp.com",也可自行从配置文件读取
        string adRootDomain = "yourdomain.com";
        String DomName = "dc=" + adRootDomain.Replace(".", ",dc="); 

        using (var searchRoot = new DirectoryEntry("LDAP://" + DomName))
        using (var searcher = new DirectorySearcher(searchRoot, "(&(ObjectClass=msExchDynamicDistributionList)(proxyAddresses=smtp:" + strEmailAddress.Trim() + "))"))
        using (var results = searcher.FindAll())
        {
            foreach (SearchResult result in results)
            {
                if (result.Properties.Contains("cn") && result.Properties.Contains("msExchDynamicDLFilter"))
                {
                    DistributionList dl = new DistributionList();
                    // 正确实例化嵌套结构体,标记为动态通讯组类型
                    dl.DType = new DistributionList.DLT { DDL = "DynamicDistributionList" }; 
                    dl.CN = result.Properties["cn"][0].ToString();
                    dl.FILORDN = result.Properties["msExchDynamicDLFilter"][0].ToString();
                    distributionLists.Add(dl);
                }
            }
        }
        return distributionLists;
    }
}

public class DistributionList   
{
    public struct DLT
    {
        public String DDL;
    }

    public DLT DType { get; set; }
    public string CN { get; set; }
    public string FILORDN { get; set; }
    public List<string> Members { get; set; }
}

注:如果需要读取配置文件中的域名,可在App.config的<appSettings>节点添加<add key="ADRootDomain" value="你的实际域名.com"/>,再将代码中adRootDomain赋值替换为ConfigurationManager.AppSettings["ADRootDomain"]即可。

内容的提问来源于stack exchange,提问作者PoeAn Lu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 04:30:54