编程方式确定当前的域控制器 [英] Determine current domain controller programmatically

查看:211
本文介绍了编程方式确定当前的域控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要查询当前的域控制器,可能主要以更改用户密码。

I need to query current domain controller, probably primary to change user password.

(P)直流名应该是完全合格的,即 DC = PDC,DC =例如,DC = COM (如何正确的名称,符号?)

(P)DC name should be fully qualified, i.e. DC=pdc,DC=example,DC=com (how to properly name such notation?)

怎样才可以使用C#做了什么?

How can it be done using C#?

推荐答案

(需要System.DirectoryServices.AccountManagement.dll):

(requires System.DirectoryServices.AccountManagement.dll):

using (var context = new System.DirectoryServices.AccountManagement.PrincipalContext(ContextType.Domain))
{
    string server = context.ConnectedServer; // "pdc.examle.com"
    string[] splitted = server.Split('.'); // { "pdc", "example", "com" }
    IEnumerable<string> formatted = splitted.Select(s => String.Format("DC={0}", s));// { "DC=pdc", "DC=example", "DC=com" }
    string joined = String.Join(",", formatted); // "DC=pdc,DC=example,DC=com"

    // or just in one string

    string pdc = String.Join(",", context.ConnectedServer.Split('.').Select(s => String.Format("DC={0}", s)));
}

这篇关于编程方式确定当前的域控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆