【问题标题】:Get Department of user from AD从 AD 获取用户部门
【发布时间】:2018-06-26 09:44:41
【问题描述】:

在 ASP.net CORE MVC 网站中使用 Active Directory, 我可以获得许多用户属性,例如 diplayName、emailAdress ... 但是我找不到用户所在的部门。
获取用户信息

UserPrincipal user = UserPrincipal.FindByIdentity(new PrincipalContext(ContextType.Domain), Environment.UserName);

但用户没有“部门”属性。

我试过了:

DirectoryEntry directoryEntry = user.GetUnderlyingObject() as DirectoryEntry;
        var property = "department";
        if (directoryEntry.Properties.Contains(property))
        {
            var dep = directoryEntry.Properties[property].Value.ToString();
        }

也没有部门财产。

编辑

这里是可用属性列表:"objectClass, cn, sn, title, description, userCertificate, givenName, distinguishedName, instanceType, whenCreated, whenChanged, displayName, uSNCreated, memberOf, uSNChanged, proxyAddresses, homeMDB, mDBUseDefaults, mailNickname, name, objectGUID, userAccountControl, badPwdCount, codePage, countryCode, badPasswordTime, lastLogon, pwdLastSet, primaryGroupID, objectSid, accountExpires, logonCount, sAMAccountName, sAMAccountType, showInAddressBook, legacyExchangeDN, userPrincipalName, objectCategory, dSCorePropagationData, lastLogonTimestamp, textEncodedORAddress, mail and Lot of msExchange"

【问题讨论】:

  • 可能与AD条目有关,也可能与您的代码有关。你的代码对我来说看起来不错。如果 AD 数据没有您想要检查的值,我建议您详细说明问题所在。

标签: c# asp.net-core active-directory


【解决方案1】:

这对我有用(混合使用 AccountManagementDirectoryServices):

var ad = new PrincipalContext(ContextType.Domain, DOMAIN);
var u  = new UserPrincipal(ad) {SamAccountName = Environment.UserName};

using (var search = new PrincipalSearcher(u))
{
    var user = (UserPrincipal) search.FindOne();

    DirectoryEntry dirEntry = (DirectoryEntry)user.GetUnderlyingObject();
    string dept = dirEntry.Properties["Department"].Value.ToString();
    Console.WriteLine(dept);
}

这需要以下using

using System;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;

【讨论】:

  • 不好,我没有任何“部门”属性,检查编辑。
  • @Alexis 如果您没有该属性,您打算如何访问它???
  • @bradbury9 问题是我不知道这个属性是否存在,它没有出现在我的代码中,但 IT 技术人员说它存在。
  • 要么不存在,要么尝试访问它的用户没有权限。我会与 IT 部门再次核对。
  • 我已经和 IT 核实过了,我的部门没有进入,所以问题解决了,我把你的答案作为正确的。如果字段未填写,则不会出现。为什么只是微软让它看起来是空的?
【解决方案2】:

搜索员工所在单位

使用价值

Department = directoryEntry.Properties["subdivision"].Value as string,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-23
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    相关资源
    最近更新 更多