首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在给定UserPrincipal对象的情况下从Active Directory获取"公司"和"部门"?

在给定UserPrincipal对象的情况下,从Active Directory获取"公司"和"部门"的方法如下:

  1. 首先,确保已经安装了System.DirectoryServices.AccountManagement命名空间。如果没有,请安装该命名空间。
  2. 使用UserPrincipal对象,可以通过以下方法获取"公司"和"部门"信息:
代码语言:csharp
复制
using System;
using System.DirectoryServices.AccountManagement;

public static void GetCompanyAndDepartment(UserPrincipal userPrincipal)
{
    // 获取UserPrincipal的DirectoryEntry对象
    DirectoryEntry directoryEntry = userPrincipal.GetUnderlyingObject() as DirectoryEntry;

    // 获取"公司"属性
    string company = directoryEntry.Properties["company"].Value.ToString();

    // 获取"部门"属性
    string department = directoryEntry.Properties["department"].Value.ToString();

    Console.WriteLine($"公司: {company}");
    Console.WriteLine($"部门: {department}");
}
  1. 调用GetCompanyAndDepartment方法,传入UserPrincipal对象:
代码语言:csharp
复制
UserPrincipal userPrincipal = // 获取UserPrincipal对象;
GetCompanyAndDepartment(userPrincipal);

这样,就可以从Active Directory中获取"公司"和"部门"信息了。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券