c# - 如何确定计算机是否连接到 novell eDirectory 或 Microsoft ActiveDirectory?

标签 c# .net active-directory novell edirectory

我刚刚在我的应用程序中实现了 Novell eDirectory。由于我们的应用程序支持 Microsoft ActiveDirectory,因此我想阻止一个额外的配置参数,如“Novell yes/no”。

那么,是否有另一种方法可以确定计算机是连接到 Microsoft ActiveDirectory 还是 Novell 网络?

最佳答案

如果您想知道计算机是否属于 Windows 域,您可以获取 Win32_NTDomain WMI 信息。

在 powerShell 中它给出:

Get-WmiObject Win32_NTDomain
ClientSiteName          : Default-First-Site-Name
DcSiteName              : Default-First-Site-Name
Description             : DOM
DnsForestName           : dom.fr
DomainControllerAddress : \\192.168.183.100
DomainControllerName    : \\WM2008R2ENT
DomainName              : DOM
Roles                   :
Status                  : OK

版本根据 @ScottTx评论你也可以使用 Win32_ComputerSystem WMI 类

PS> (Get-WMIObject Win32_ComputerSystem).PartOfDomain
False

根据 Win32_NTDomain class documentation在 C# 中,您可以通过以下方式获取它:

using System;
using System.Collections.Generic;
using System.Text;

using System.Management;

namespace WMIQuery
{
  class WmiQuery
  {
    static void Main(string[] args)
    {
      ManagementObjectSearcher domainInfos = new ManagementObjectSearcher("select * from WIN32_NTDomain");

      foreach (ManagementObject domainInfo in domainInfos.Get())
      {
        Console.WriteLine("Name : {0}", domainInfo.GetPropertyValue("Name"));
        Console.WriteLine("Computer/domain : {0}", domainInfo.GetPropertyValue("Caption"));
        Console.WriteLine("Domain name : {0}", domainInfo.GetPropertyValue("DomainName"));
        Console.WriteLine("Status : {0}", domainInfo.GetPropertyValue("Status"));
      }

      // Edition according to @ScottTx comment you can also use `Win32_ComputerSystem` WMI class

      ManagementObjectSearcher ComputerInfos = new ManagementObjectSearcher("select * from Win32_ComputerSystem");
      foreach (ManagementObject ComputerInfo in ComputerInfos.Get())
      {
        if ((bool)ComputerInfo.GetPropertyValue("PartOfDomain"))
          Console.WriteLine("This computer is part of domain");
        else
          Console.WriteLine("This computer is not part of domain");
      }
    }
  }
}

添加对 System.Management 程序集的引用

关于c# - 如何确定计算机是否连接到 novell eDirectory 或 Microsoft ActiveDirectory?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5964390/

相关文章:

c# - 仅更新 Entity Framework 中实际更新的字段

c# - 将新列中的按钮添加到 DataGrid 中的所有行

c# - 如何以编程方式设置 X509Certificate2 的 PIN?

node.js - 使用azure隐式流检索oauth访问 token

c# - 在 ASP.Net C# Webform 的 HTML 表格中显示 DataTable

.net - 监视 IIS 站点重新编译 .NET 3.5

c# - C# 的对象缓存

.net - GAC 文件夹 - 太多?

asp.net-mvc - 使用AD的基于ASP.NET MVC表单的Auth可以在本地工作,但在服务器(iis7)上将失败

c# - 生产环境中出现 DirectoryServicesComException,但本地计算机上没有 C# ASP.net