【发布时间】:2018-04-03 21:33:22
【问题描述】:
我想用C#来
- 创建一个新用户。
- 将其添加到“用户”组
- 启用拨入远程网络权限
- 禁用远程控制
我的代码:
PrincipalContext ctx = new PrincipalContext(ContextType.Machine);
UserPrincipal newuser = new UserPrincipal(ctx, "newuser", "password", true);
newuser.Save();
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx,"Users");
using (DirectoryEntry groupEntry = group.GetUnderlyingObject() as DirectoryEntry )
using (DirectoryEntry userEntry = newuser.GetUnderlyingObject() as DirectoryEntry)
{
groupEntry.Invoke( "Add", new object[] { userEntry.Path } );
userEntry.RefreshCache();
userEntry.Properties["msNPAllowDialin"].Value = true;
userEntry.Properties["msTSRemoteControl"].Value = false;
userEntry.CommitChanges();
}
但是,帐户创建和用户组过程可以正常工作,
userEntry.Properties["msNPAllowDialin"].Value = true;
userEntry.Properties["msTSRemoteControl"].Value = false;
两者都会产生错误
在缓存中找不到目录属性
我用谷歌搜索了 2 个小时,但仍然找不到任何可行的解决方案。
【问题讨论】:
-
在我看来,您正在寻找 LDAP 中不存在的属性。你在哪里运行这段代码?服务器详情?
-
我正在运行 Windows server 2016,但我只想创建可以访问服务器 VPN 的本地用户。
标签: c# .net directoryentry userprincipal