C# - 不确定如何修复与其他人复制的自定义类/类型有关的错误
我正在尝试从Active Directory获取动态发行组的列表,有人指向我堆栈溢出链接使用据说有效的代码,但我不是100%确定如何使该代码的所有部分都起作用,因为该链接仅包含其代码的一部分。我在底部添加了几个使用的语句和几个结构和几个结构和发行列表类,但是我不确定如何摆脱设置的错误。ImpersonatedUserDomain Line抛出的抛出,因为它说“模糊的使用域”并未在'设置'。我也遇到了麻烦
dl.dtype = new dlt.ddl.set();
因为它说“ ddl”的类型名称在类型的'class2.dlt'中不存在,即使我已经在下面的结构中定义了它。我不确定我在这里做错了什么。我会评论堆栈溢出问题,但我没有足够的声誉。谁能帮忙?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.DirectoryServices;
using System.Configuration;
using System.CodeDom;
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;
using NuGet.Configuration;
/* This code may only be usable over a VPN*/
/* Source: https://stackoverflow.com/questions/53431322/looking-for-help-accessing-dynamic-distribution-lists-using-c-sharp-and-adws*/
public class Class2
{
//public object Settings { get; private set; }
public List<DistributionList> GetDynamicDistributionLists(string strEmailAddress)
{
List<DistributionList> distributionLists = new List<DistributionList>();
String DomName = "dc=" + Settings.ImpersonatedUserDomain.Replace(".", ",dc=");
using (var group = new DirectoryEntry("LDAP://" + DomName))
{
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 DLT.DDL.set();
dl.CN = result.Properties["cn"][0].ToString(); //
dl.FILORDN = result.Properties["msExchDynamicDLFilter"][0].ToString();
distributionLists.Add(dl);
}
}
}
}
return distributionLists;
}
public struct DLT
{
public List<String> DDL { get; set; }
}
}
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; }
}
I am trying to fetch the list of dynamic distribution groups from an Active Directory, and someone pointed me to a Stack Overflow link with code that supposedly works, but I am not 100% sure how to make all the parts of this code work, as the link only contains a portion of their code. I added a couple of using statements and a couple of structs and the DistributionList class at the bottom, but I am unsure of how to get rid of the error that the Settings.ImpersonatedUserDomain line throws because it says that 'ImpersonatedUserDomain' is not defined in 'Settings'. I also am having trouble with the line
dl.DType = new DLT.DDL.set();
because it says that the type name 'DDL' does not exist in the type 'Class2.DLT', even though I have already defined it in the struct below. I'm not sure what I am doing wrong here. I would comment on the Stack Overflow question, but I don't have enough reputation. Can anyone help?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.DirectoryServices;
using System.Configuration;
using System.CodeDom;
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;
using NuGet.Configuration;
/* This code may only be usable over a VPN*/
/* Source: https://stackoverflow.com/questions/53431322/looking-for-help-accessing-dynamic-distribution-lists-using-c-sharp-and-adws*/
public class Class2
{
//public object Settings { get; private set; }
public List<DistributionList> GetDynamicDistributionLists(string strEmailAddress)
{
List<DistributionList> distributionLists = new List<DistributionList>();
String DomName = "dc=" + Settings.ImpersonatedUserDomain.Replace(".", ",dc=");
using (var group = new DirectoryEntry("LDAP://" + DomName))
{
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 DLT.DDL.set();
dl.CN = result.Properties["cn"][0].ToString(); //
dl.FILORDN = result.Properties["msExchDynamicDLFilter"][0].ToString();
distributionLists.Add(dl);
}
}
}
}
return distributionLists;
}
public struct DLT
{
public List<String> DDL { get; set; }
}
}
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; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我带领你走这条路,所以我可能应该为您提供帮助。
I led you down this path, so I should probably help you. ????
That code has some things specific to their implementation that you don't need.
Settings.ImpersonatedUserDomain
seems to be a setting they created that gets pulled from somewhere else. But it's just the name of the domain. I have no idea whatDLT.DDL.set()
does, but you probably don't need it.You should also be looking at the
msExchDynamicDLBaseDN
attribute too. It may turn out to be the same on every DL, but just in case...That code also takes the email address of one dynamic DL and gets the details of just that one, where, if I remember correctly, you want to get all of the dynamic DLs on your domain. If I'm correct in that, this is how I would do it:
Set
domain
to the name of your domain. This will populate theMembers
collection with thedistinguishedName
of each member. You can change that if you'd rather have the email (use themail
attribute), or anything else.