Active Directory,枚举用户组,COM 异常

Active Directory, enumerating user#39;s groups, COM exception(Active Directory,枚举用户组,COM 异常)
本文介绍了Active Directory,枚举用户组,COM 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在通过 AD .NET API 枚举当前用户组时,我有时会得到

COMException: 未知错误 (0x80005000)

这是我的代码:

 var userName = Environment.UserName;var context = new PrincipalContext(ContextType.Domain);var user = UserPrincipal.FindByIdentity(context, userName);foreach (var userGroup in user.GetGroups()){Console.WriteLine(userGroup.Name);}

有什么问题吗?我以为每个用户都可以检索 HIS 组的列表?这似乎是奇怪的行为,有时可以像这样重现:在userA"PC 上运行时,它崩溃了,但它正在成功枚举其他userB"组(在用户A')!

解决方案

尝试使用

var context = new PrincipalContext(ContextType.Domain, "yourcompany.com", "DC=yourcompany,DC=com", ContextOptions.Negotiate);

将 ContextOption 设置为 Negotioate 后,客户端使用 Kerberos 或 NTLM 进行身份验证,因此即使未提供用户名和密码,帐户管理 API 也会使用调用线程的安全上下文绑定到对象.

while enumerating current user's groups through AD .NET API I sometimes get

COMException: Unknown error (0x80005000)

Here's my code :

        var userName = Environment.UserName;

        var context = new PrincipalContext(ContextType.Domain);
        var user = UserPrincipal.FindByIdentity(context, userName);

        foreach (var userGroup in user.GetGroups())
        {
            Console.WriteLine(userGroup.Name);
        }

What's the problem? I thought every user can retrieve list of HIS groups?It seems to be strange behavior, sometimes It can be reproduced like this : when running on 'userA' PC, It crashes, but it is enumerating OTHER 'userB' groups successfully (under 'userA')!

解决方案

Try using

var context = new PrincipalContext(ContextType.Domain, "yourcompany.com", "DC=yourcompany,DC=com", ContextOptions.Negotiate);

With the ContextOption set to Negotioate the client is authenticated by using either Kerberos or NTLM so even if the user name and password are not provided the account management API binds to the object by using the security context of the calling thread.

这篇关于Active Directory,枚举用户组,COM 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

What#39;s the difference between retrieving WindowsPrincipal from WindowsIdentity and Thread.CurrentPrincipal?(从 WindowsIdentity 和 Thread.CurrentPrincipal 检索 WindowsPrincipal 之间有什么区别?)
How do I find a user#39;s Active Directory display name in a C# web application?(如何在 C# Web 应用程序中查找用户的 Active Directory 显示名称?)
How to use Servicestack Authentication with Active Directory/Windows Authentication?(如何在 Active Directory/Windows 身份验证中使用 Servicestack 身份验证?)
How can I authenticate against Active Directory in Nancy?(如何在 Nancy 中对 Active Directory 进行身份验证?)
How to get a username in Active Directory from a display name in C#?(如何从 C# 中的显示名称获取 Active Directory 中的用户名?)
Oauth 2 token for Active Directory accounts(Active Directory 帐户的 Oauth 2 令牌)