【问题标题】:C# DirectoryEntry Properties Value ChangeC# DirectoryEntry 属性值更改
【发布时间】:2018-04-03 21:33:22
【问题描述】:

我想用C#来

  1. 创建一个新用户。
  2. 将其添加到“用户”组
  3. 启用拨入远程网络权限
  4. 禁用远程控制

我的代码:

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


【解决方案1】:

您正在创建本地计算机帐户 (ContextType.Machine),但随后尝试设置特定于 Active Directory 用户帐户的属性。那是行不通的。

如果您想限制他们访问服务器的 VPN,那么这取决于您使用的 VPN 类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 2021-11-24
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多