C# 如何在Windows窗体中使用System.DirectoryServices获取部门名称、经理、角色标题

C# 如何在Windows窗体中使用System.DirectoryServices获取部门名称、经理、角色标题,c#,C#,我是C#脚本开发新手,我试图通过添加reference system.directoryservices在windows表单标签中获取部门名称、经理名称和角色标题。任何人都可以支持我,如何在表单标签中获取详细信息。我不知道管理器是如何存储的,但您可以调试下面的方法,查看结果并找出您要查找的内容 下面的示例演示如何访问用户信息: using System; using System.Collections.Generic; using System.Linq; using System.Windo

我是C#脚本开发新手,我试图通过添加reference system.directoryservices在windows表单标签中获取部门名称、经理名称和角色标题。任何人都可以支持我,如何在表单标签中获取详细信息。

我不知道管理器是如何存储的,但您可以调试下面的方法,查看结果并找出您要查找的内容

下面的示例演示如何访问用户信息:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    //from nuget you need this 2
    using System.DirectoryServices;
    using System.DirectoryServices.AccountManagement;
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            GetUserAD();
        }

        public void GetUserAD()
        {

            var userDto = new UserDto();

            using PrincipalContext pc = new PrincipalContext(ContextType.Domain);
            UserPrincipal up = UserPrincipal.FindByIdentity(pc, Environment.UserName);


            userDto.FirstName = up.GivenName;
            userDto.LastName = up.Surname;
            userDto.UserEmail = up.EmailAddress;
            userDto.LastLogon = up.LastLogon;
            userDto.FixPhone = up.VoiceTelephoneNumber;
            userDto.UserDisplayName = up.DisplayName;
            userDto.JobTitle = up.Description;

            DirectoryEntry directoryEntry = up.GetUnderlyingObject() as DirectoryEntry;
            userDto.Department = directoryEntry.Properties["department"]?.Value as string;
            userDto.MobilePhone = directoryEntry.Properties["mobile"]?.Value as string;
            userDto.MemberOf = directoryEntry.Properties["memberof"]?.OfType<string>()?.ToList();
        }
    }
    public class UserDto
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string UserEmail { get; set; }
        public string LastLogon { get; set; }
        public string FixPhone { get; set; }
        public string UserDisplayName { get; set; }
        public string JobTitle { get; set; }
        public string Department { get; set; }
        public string MobilePhone { get; set; }
        public List<string> MemberOf { get; set; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Windows.Forms;
命名空间WindowsFormsApp1
{
//从nuget你需要这个2
使用System.DirectoryServices;
使用System.DirectoryServices.AccountManagement;
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
GetUserAD();
}
public void GetUserAD()
{
var userDto=new userDto();
使用PrincipalContext pc=新PrincipalContext(ContextType.Domain);
UserPrincipal up=UserPrincipal.FindByIdentity(pc、Environment.UserName);
userDto.FirstName=up.GivenName;
userDto.LastName=up.LastName;
userDto.UserEmail=up.EmailAddress;
userDto.LastLogon=up.LastLogon;
userDto.FixPhone=up.VoiceTelephoneNumber;
userDto.UserDisplayName=up.DisplayName;
userDto.JobTitle=up.Description;
DirectoryEntry DirectoryEntry=up.GetUnderlineingObject()作为DirectoryEntry;
userDto.Department=directoryEntry.Properties[“Department”]?。值为字符串;
userDto.MobilePhone=directoryEntry.Properties[“mobile”]?。值为字符串;
userDto.MemberOf=directoryEntry.Properties[“MemberOf”]?.OfType()?.ToList();
}
}
公共类UserDto
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串UserEmail{get;set;}
公共字符串LastLogon{get;set;}
公共字符串FixPhone{get;set;}
公共字符串UserDisplayName{get;set;}
公共字符串JobTitle{get;set;}
公共字符串部门{get;set;}
公共字符串移动电话{get;set;}
{get;set;}的公共列表成员
}
}

欢迎来到SO,请查看有关如何提问的教程