【问题标题】:Change AD user Terminal Server properties with C#使用 C# 更改 AD 用户终端服务器属性
【发布时间】:2015-04-21 13:37:35
【问题描述】:

我正在使用 System.DirectoryServices.DirectoryEntry 创建 AD 用户,除某些远程桌面特定属性外,一切正常。

示例:

newUser.Properties["msTSConnectClientDrives"].Value = false;
newUser.Properties["msTSConnectPrinterDrives"].Value = false;
newUser.Properties["msTSDefaultToMainPrinter"].Value = false;

这不会引发任何异常,所以我猜想在对象中找到了属性,但它们没有任何效果。当我进入该用户的属性窗口时,在“环境”选项卡下,这 3 个复选框仍处于选中状态。

我是否缺少这些属性的特殊内容?

感谢您的帮助。

编辑:

对不起,我真的很忙,这是一个代码示例:

    private string CreateNewADAccount(string accountName, string accountPassword)
    {
        try
        {
            PrincipalContext context = new PrincipalContext(ContextType.Domain, "SV-LITE", @"LITE\xxxxxxxx", "yyyyyyyy");

            UserPrincipal newUser = new UserPrincipal(context);
            newUser.SamAccountName = accountName;
            newUser.UserPrincipalName = accountName;
            newUser.Name = "LiteUser2015 - " + accountName;
            newUser.DisplayName = "LiteUser2015 - " + accountName;
            newUser.SetPassword(accountPassword);
            newUser.PasswordNeverExpires = true;
            newUser.UserCannotChangePassword = true;

            newUser.Save();

            // Set advanced properties
            if (newUser.GetUnderlyingObjectType() == typeof(DirectoryEntry))
            {
                DirectoryEntry entry = (DirectoryEntry)newUser.GetUnderlyingObject();

                entry.Properties["msTSConnectClientDrives"].Value = false;
                entry.Properties["msTSConnectPrinterDrives"].Value = false;
                entry.Properties["msTSDefaultToMainPrinter"].Value = false;
                entry.Properties["msTSInitialProgram"].Value = "test";

                entry.CommitChanges();
            }

            return newUser.Guid.ToString();

        }
        catch (Exception e)
        {
            MessageBox.Show("Failed to create PrincipalContext. Exception: " + e);
        }

        return null;
    }

【问题讨论】:

    标签: c# properties directoryentry


    【解决方案1】:

    进行更改后,您必须调用 CommitChanges - newUser.CommitChanges();

    https://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry.commitchanges%28v=vs.110%29.aspx

    默认情况下,对属性的更改是在本地缓存中进行的。

    【讨论】:

    • 你能用更完整的代码示例更新你的问题吗?
    【解决方案2】:

    这可能与您使用的服务器操作系统版本有关。我在another site 上找到了这个关于 Windows 2000 和 2003 的答案。它应该适用于 Windows2008 及更高版本:

    对于 2000 / 2003,您必须使用终端服务访问它们 ADSI 扩展。参考在这里:

    http://msdn.microsoft.com/en-us/library/aa380823(VS.85).aspx

    【讨论】:

    猜你喜欢
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多