GroupPrincipal 和 SAM PrimaryContext 出现无法解释的 InvalidOperationException
我试图创建一个像这样的主体:
PrincipalContext pc = new PrincipalContext(ContextType.Machine);
GroupPrincipal group = new GroupPrincipal(pc);
group.Name = "Some Group Name";
group.Description = "Some Group Name Description";
group.Save();
但是,当执行代码时,我收到以下异常消息:
System.DirectoryServices.AccountManagement: 该属性对该商店无效 类型。
如果我不设置Description
属性,上面的代码工作得很好,只是没有组的描述。
我做错了什么吗?
提前致谢。
编辑: 我相信我已经找到了解决这个问题的方法(对于任何可能感兴趣的人)。 可以按照与上述相同的方式创建组:
PrincipalContext pc = new PrincipalContext(ContextType.Machine);
GroupPrincipal group = new GroupPrincipal(pc);
group.Save();
现在您创建一个 DirectoryEntry
并将其链接到新创建的组,如下所示:
string path = "WinNT://" + machineName + "/" + group.SamAccountName;
DirectoryEntry dEntry = new DirectoryEntry(path);
这允许访问该组的属性,但我是感兴趣的是描述,所以:
dEntry.Properties["description"].Add("Some Decription");
dEntry.CommitChanges();
那就应该可以了。
I am trying to create a Principal like this:
PrincipalContext pc = new PrincipalContext(ContextType.Machine);
GroupPrincipal group = new GroupPrincipal(pc);
group.Name = "Some Group Name";
group.Description = "Some Group Name Description";
group.Save();
However, when the code is executed, I get the following exception message:
System.DirectoryServices.AccountManagement:
Property is not valid for this store
type.
If I do not set the Description
property, the above code works perfectly fine, just doesn’t have a description for a group.
Am I doing something wrong?
Thanks in advance.
EDIT:
I believe I have found a work-around for this (for anyone who maybe interested).
A group can be created in the same way as above:
PrincipalContext pc = new PrincipalContext(ContextType.Machine);
GroupPrincipal group = new GroupPrincipal(pc);
group.Save();
Now you create a DirectoryEntry
and link it to the newly created Group like this:
string path = "WinNT://" + machineName + "/" + group.SamAccountName;
DirectoryEntry dEntry = new DirectoryEntry(path);
This allows access to the Properties of that group, but the one I was interested in is Description, so:
dEntry.Properties["description"].Add("Some Decription");
dEntry.CommitChanges();
And that should do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案已重写
我已经得到了您的答案,但您可能不太喜欢它。互联网上的信息很少,但在代码中,这是可以解释的:
在 SAMStoreCtx 内部,有一段代码如下(Reflector 输出):
none
进行比较的行将始终生成 ObjectMask.None。因此,第二个 if 语句始终为真。我相信这是 Microsoft 库中的一个错误。它仅发生在 SAMStoreCtx 中。也许这是故意的,但因为代码在那里但总是返回 false 让我相信程序员打算使用 or 运算符。使用 DisplayName 等其他属性检查我的发现会引发与预期相同的异常。
您可以联系 Microsoft 询问此问题并向他们展示此主题。我还没有检查过 .NET 4.0 的新测试版,它们的显示可能会有所不同。您可以通过下载 Reflector 并加载相关的 .NET 程序集来亲自检查这一点。
编辑:我已为您联系 Microsoft 并通过 connect.microsoft 报告了该错误.com 这里。如果您愿意,可以关注该问题。
Answer rewritten
I've got your answer, but you may not really like it. The information on the internet is scarce, but in code, it is explainable:
Inside SAMStoreCtx, there's a piece of code that looks as follows (Reflector output):
none
using the and-operator will always result in ObjectMask.None. The second if-statement is therefor always true.I believe this to be a bug in the Microsoft library. It only happens with the SAMStoreCtx. Perhaps it is on purpose, but because the code is there but always returns false makes me believe the programmers intended the use of the or-operator instead. Checking my findings with other properties like DisplayName throws the same exception as expected.
You can contact Microsoft about this and show them this thread. I haven't checked the new betas of .NET 4.0 which might show up differently. You can check this for yourself by downloading Reflector and loading the relevant .NET assembly.
EDIT: I've contacted Microsoft for you and reported the bug through connect.microsoft.com here. You can follow the issue there if you like.
您可以使用 GetUnderlyingObject 获取该组的 DirectoryEntry,而不是搜索它。
You can use the GetUnderlyingObject to get the DirectoryEntry for the group instead of searching for it.