gpt4 book ai didi

c# - 事件目录 : How to find if a domain is available?

转载 作者:太空狗 更新时间:2023-10-30 00:12:29 25 4
gpt4 key购买 nike

在使用 GetDomain 之前,有什么方法可以查明某个域在 ActiveDirectory 中是否可用?我有一个应用程序,用户应该能够自己添加域,如果他们输入无效的域,应该会出现错误。现在它是通过捕获下面的异常来处理的,但是用户输入无效域几乎不是异常事件,并且异常也可能需要很长时间才能被抛出,尤其是如果输入了 ip 地址(看起来像) .对此有更好的解决方案吗?

public Domain RegisterUserDirectory(string domainId) {
DirectoryContext context = new DirectoryContext(DirectoryContextType.Domain, domainId);

System.DirectoryServices.ActiveDirectory.Domain domain;
try {
domain = System.DirectoryServices.ActiveDirectory.Domain.GetDomain(context);
}
catch (ActiveDirectoryNotFoundException adne) {
// handle
}
catch (Exception e) {
Log.Warning("Failed to contact domain {0}: {1}", domainId, e.Message);
throw;
}

...
...
}

最佳答案

我能想到的唯一其他选择是使用林来枚举域。即

 var myDomain = Domain.GetCurrentDomain();  //or .GetComputerDomain();
var forestDomains = myDomain.Forest.Domains;

这假设您想要的所有域都在同一个林中。然后,您需要针对此集合测试用户输入的 domainId,可能针对每个域 .Name 属性进行测试。

关于c# - 事件目录 : How to find if a domain is available?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4666334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com