c# - 通过 c#/.Net 从 AD 检索特定用户详细信息

标签 c# asp.net authentication active-directory sspi

我尝试使用以下方法检索特定 ADGroup 内用户的 SamAccountNameSurnameGivenName:

        PrincipalContext principalContext = new PrincipalContext(ContextType.Domain);
        GroupPrincipal group = GroupPrincipal.FindByIdentity(principalContext, adgroup);

        foreach (Principal principal in group.Members)
        {
            samName = principal.SamAccountName;
            surName = principal.SurName;   <-- Intellisense gives error
            givenName = principal.GivenName;   <-- Intellisense gives error
        }

当我单步执行代码并在 Visual Studio 中为上述内容添加监视时,它们显示正确的信息,但 principal.Surnameprincipal.GivenName 给出以下内容编译时出错:

“Principal”不包含“____”的定义,并且找不到扩展方法

有人可以解释为什么我在 IDE 中使用代码中断并将鼠标悬停在 principal 变量上时可以看到信息,但无法访问代码中的属性吗?

最佳答案

根据 docs

SurNameGivenName 不是 Principal 类型的公共(public)属性。

看起来您需要 UserPrincipal 类来公开这些属性,请参阅 UserPrincipal documentation

我现在无法100%验证,但我想如果你改变

foreach (Principal principal in group.Members)

foreach (UserPrincipal principal in group.Members)

应该可以

关于c# - 通过 c#/.Net 从 AD 检索特定用户详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42725742/

相关文章:

c# - 在命令文本中设置 ANSI_NULLS 会出现错误

c# - 单声道 ASP.NET 异常

firebase - 为什么云函数 URL (Firebase) 可以公开访问?如何限制访问特定的外部调用?

使用 SSL 登录 php

c# - 获取(OAuthException - #2500)必须使用事件访问 token 来查询有关当前用户的信息

c# - Entity Framework 更新未保存

c# - 将 Func<Type1, bool> 映射到 Func<Type2, bool>

.net - 如何在 aspx 页面上动态显示视频播放器

asp.net - 从 ASP.NET 服务器控件动态添加 CSS 文件

facebook - 使用 oAuth 登录,我应该存储/使用什么来识别用户?