![](/img/trans.png)
[英]System.DirectoryServices.AccountManagement.UserPrincipal - ChangePassword method throws exception
[英]System.DirectoryServices.AccountManagement.Principal.Delete() throws unclear exception
我目前正在为我所在的公司创建ActiveDirectory管理工具包。
在编写函数以使用System.DirectoryServices.AccountManagement读取和处理组成员资格及其附加的嵌套组时,我抛出了一个奇怪的异常。
这是代码:
var nestedgroups = user.GetAuthorizationGroups();
var groups = user.GetGroups();
try
{
foreach(Principal p in groups)
{
foreach(Principal np in nestedgroups)
{
if (p.Equals(np))
{
np.Delete();
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
nestedgroups
表示UserPrincipal.GetAuthorizationGroups()的结果;
groups
表示UserPrincipal.GetGroups()的结果;
该功能的目的是从已存在于groups
nestedgroups
groups
删除所有主体。 这样做是为了提高在进一步处理和输出分配给相关用户的组时的性能。
if块的条件为true,因此Principal.Delete();
,针对由foreach循环当前处理的Principal,应该执行得很好。 问题就从这里开始。 一旦应该运行Delete()函数,它就会引发以下异常:
Exception thrown: 'System.DirectoryServices.AccountManagement.PrincipalOperationException' in System.DirectoryServices.AccountManagement.dll
System.DirectoryServices.AccountManagement.PrincipalOperationException: A device attached to the system is not functioning.
在代码继续运行的同时,Principal不会从集合中删除(如果措辞不正确,请更正我)。
我已经将异常输入到Google中,但没有成功。
如果您可以根据您的个人经验为我提供更多资源或提示,以解决/避免此错误,我将非常高兴。
创建一个新列表,然后在返回之前将有效条目添加到该列表中。 原始列表不必突变
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.