eri*_*rik 4 c# active-directory
在使用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;
}
...
...
}
Run Code Online (Sandbox Code Playgroud)
我能想到的唯一其他选择是使用林来枚举域.即
var myDomain = Domain.GetCurrentDomain(); //or .GetComputerDomain();
var forestDomains = myDomain.Forest.Domains;
Run Code Online (Sandbox Code Playgroud)
这假设您想要的所有域都在同一个林中.然后,您需要domainId针对此集合测试您的用户,可能针对每个域.Name属性进行测试.