繁体   English   中英

使用C#创建名为“#”的Active Directory组作为其名称中的第一个字符

[英]Create Active Directory Group with '#' as the first character in its name by using C#

我想创建一个新的Active Directory组,并使用'#'作为其名称中的第一个字符。 但我收到一条异常消息,说我的C#代码中有一个无效的'dn'。 我知道'#'是powershell脚本中的一个特殊字符,然后我用单引号转义'#',我的C#代码也没有例外,新的Active Directory组也成功创建。 但单引号也显示在Active Directory中。

string name = "#ABC";
public void Create(string ouPath, string name)
{
    if (!DirectoryEntry.Exists("LDAP://CN=" + name + "," + ouPath))
    {
         try
         {
            DirectoryEntry entry = new DirectoryEntry("LDAP://" + ouPath);
            DirectoryEntry group = entry.Children.Add("CN=" + name, "group");
            group.Properties["sAmAccountName"].Value = name;
            group.CommitChanges();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message.ToString());
        }
    }
    else { Console.WriteLine(path + " already exists"); }
}

在此输入图像描述

是否有人可以帮我创建名为“#”的Active Directory组作为其名称中的第一个字符?

谢谢。

不允许#作为DN或CN的第一个字符。 这是Active Directory的限制。

请参阅此处了解保留字符。

如链接文章末尾所述,您必须使用反斜杠( \\ )而不是单引号来转义#
请注意,在C#中,字符串中的反斜杠会被另一个反斜杠转义。 所以你的组名字符串应该是这样的:

string name = "\\#ABC"; 
string ouPath = // your ou path
Create(ouPath, name);

更新:另一种转义保留字符的方法是通过反斜杠和十六进制ascii代码, #0x23 所以你的例子中的字符串应该是:

string name = "\\23ABC";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM