我不是天生的程序员,所以我提前道歉 :) 我正在使用来自 http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C#39 的代码片段这真的很有帮助。我正在使用他的方法获取用户组成员资格,它也需要他的 AttributeValuesMultiString
方法。我没有任何语法错误,但是当我通过 Groups("username", true)
调用 Groups
方法时,出现以下错误:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in System.DirectoryServices.dll
我已经做了一些挖掘,但似乎没有什么能真正回答我为什么会收到这个错误。
最佳答案
您应该查看 System.DirectoryServices.AccountManagement
(S.DS.AM) 命名空间。在这里阅读所有相关信息:
- Managing Directory Security Principals in the .NET Framework 3.5
- MSDN docs on System.DirectoryServices.AccountManagement
基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:
// set up domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
if(user != null)
{
// get the user's groups
var groups = user.GetAuthorizationGroups();
foreach(GroupPrincipal group in groups)
{
// do whatever you need to do with those groups
}
}
}
新的 S.DS.AM 使得在 AD 中与用户和组一起玩真的很容易!
关于C# 获取用户在 Active Directory 中所属的组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25751176/