DirectoryEntry 字符串作为文字工作但不作为格式化字符串?

发布于 01-07 15:08 字数 2082 浏览 3 评论 0原文

好的,所以我试图将用户从活动目录拉入 DirectoryUser 对象,如果我像这样输入它,它工作正常:

DirectoryEntry user = new DirectoryEntry(@"LDAP://CN=Name, OU=Department, OU=Group, DC=Domain1, DC=Domain2");
user.Properties["thumbnailPhoto"].Clear();

但是我需要能够更改值,所以我尝试了格式化字符串:

string ldap = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", cn, group, ou, domain1, domain2);
DirectoryEntry user = new DirectoryEntry(ldap);
user.Properties["thumbnailPhoto"].Clear();

但是这个导致错误“服务器上没有这样的对象”

构造函数接受一个字符串,并且我传递了与我字面上编写时使用的相同的精确值,为什么它以一种方式工作而不是另一种方式?!

编辑: 我只是想补充一点,我以几种不同的方式进行了双重检查,以验证构建的字符串是否与硬编码字符串相同,确实如此。我运行了调试器并检查了 user.path 值以验证字符串的存储是否完全相同。到目前为止,一切都是相同的,但一个有效,另一个无效!

更新: 我注意到,如果我直接硬编码到字符串变量:

string ldap = @"Jeremy Stafford", "IT", "QGT", "QGT", "Local";
DirectoryEntry user = new DirectoryEntry(ldap);

这工作得很好。这让我相信字符串“type”可能有问题。也许如果我可以将构建的字符串转换回原始字符串(或者更确切地说是值类型与引用类型),它会起作用吗?但我不知道该怎么做。有什么想法吗?

更新: 我进行了语义测试。这是我使用的代码:

string ldapFormatted = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", cn, group, ou, domain1, domain2);                
                var ldapHardCoded = @"LDAP://CN=Jeremy Stafford, OU=IT, OU=QGT, DC=QGT, DC=Local";
                string message;

                if (ldapFormatted.Equals(ldapHardCoded))
                {
                    message = "They're the same value\n";
                }
                else
                {
                    message = "Strings are not the same value\n";
                }

                if (ldapFormatted.GetType() == ldapHardCoded.GetType())
                {
                   message += "They are the same type";
                }
                else
                {
                    message += "They are not the same type";
                }
                message += "\n\n" + ldapFormatted + "\n" + ldapHardCoded;
                MessageBox.Show(message);

这是结果:

在此处输入图像描述

Ok so i'm trying to pull a user from active directory into a DirectoryUser object and if I type it like this, it works fine:

DirectoryEntry user = new DirectoryEntry(@"LDAP://CN=Name, OU=Department, OU=Group, DC=Domain1, DC=Domain2");
user.Properties["thumbnailPhoto"].Clear();

But I need the values to be able to be changed, so I tried a formatted string:

string ldap = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", cn, group, ou, domain1, domain2);
DirectoryEntry user = new DirectoryEntry(ldap);
user.Properties["thumbnailPhoto"].Clear();

But this causes an error "There is no such object on the server"

The constructor takes a string, and I pass in the same exact values that i was using when i wrote it literally, why is it working one way an not the other?!

EDIT:
I just wanted to add that I double checked in several different ways to verify that the built string was coming out identical to the hard coded string and it is. I ran through debugger and checked the user.path value to verify that the strings were being stored exactly the same. Everything so far is identical, but one works and the other doesn't!

UPDATE:
I noticed that if I hard code directly to a string variable:

string ldap = @"Jeremy Stafford", "IT", "QGT", "QGT", "Local";
DirectoryEntry user = new DirectoryEntry(ldap);

This works just fine. This leads me to believe that there may be a problem with the string "type". Maybe if I could convert the built string back to a primitive string (or rather a value type versus the reference type), it would work? But I have no Idea how to do that. Any ideas?

UPDATE:
I ran a semantic test. Here is the code I used:

string ldapFormatted = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", cn, group, ou, domain1, domain2);                
                var ldapHardCoded = @"LDAP://CN=Jeremy Stafford, OU=IT, OU=QGT, DC=QGT, DC=Local";
                string message;

                if (ldapFormatted.Equals(ldapHardCoded))
                {
                    message = "They're the same value\n";
                }
                else
                {
                    message = "Strings are not the same value\n";
                }

                if (ldapFormatted.GetType() == ldapHardCoded.GetType())
                {
                   message += "They are the same type";
                }
                else
                {
                    message += "They are not the same type";
                }
                message += "\n\n" + ldapFormatted + "\n" + ldapHardCoded;
                MessageBox.Show(message);

And here was the result:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

儭儭莪哋寶赑2025-01-14 15:08:44

我尝试了您的代码并填充了格式变量,它对我有用,正如您在屏幕截图中看到的那样。

  string cn = "Jeremy Stafford";
  String group = "IT";
  string ou = "QGT";
  String domain1 = "QGT";
  string domain2 = "Local";
  string ldapFormatted = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", cn, group, ou, domain1, domain2);

  var ldapHardCoded = @"LDAP://CN=Jeremy Stafford, OU=IT, OU=QGT, DC=QGT, DC=Local";
  string message;

  if (ldapFormatted.Equals(ldapHardCoded))
  {
    message = "They're the same value\n";
  }
  else
  {
    message = "Strings are not the same value\n";
  }

  if (ldapFormatted.GetType() == ldapHardCoded.GetType())
  {
    message += "They are the same type";
  }
  else
  {
    message += "They are not the same type";
  }
  message += "\n\n" + ldapFormatted + "\n" + ldapHardCoded;
  MessageBox.Show(message);

Message

您应该真正检查输入变量中的内容。也许你里面有一些看不见的字符。将输入变量转换为字节数组并打印出该数组,以更深入地了解其中的内容。

I tried your code and filled the format variables and it works for me as you can see in the screenshot.

  string cn = "Jeremy Stafford";
  String group = "IT";
  string ou = "QGT";
  String domain1 = "QGT";
  string domain2 = "Local";
  string ldapFormatted = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", cn, group, ou, domain1, domain2);

  var ldapHardCoded = @"LDAP://CN=Jeremy Stafford, OU=IT, OU=QGT, DC=QGT, DC=Local";
  string message;

  if (ldapFormatted.Equals(ldapHardCoded))
  {
    message = "They're the same value\n";
  }
  else
  {
    message = "Strings are not the same value\n";
  }

  if (ldapFormatted.GetType() == ldapHardCoded.GetType())
  {
    message += "They are the same type";
  }
  else
  {
    message += "They are not the same type";
  }
  message += "\n\n" + ldapFormatted + "\n" + ldapHardCoded;
  MessageBox.Show(message);

Message

You should really check whats in your input variables. Maybe you have some invisible chars in it. Convert your input variables into byte arrays and print out the array to get more insight what you have in there.

疧_╮線2025-01-14 15:08:44

我相信硬编码字符串有问题,因为如果将硬编码值复制到变量 cn, group, ... 你就得到了工作测试。因此,请分享一段初始化变量cn, group, ou, domain1, domain2的代码。

下面的测试代码工作正常。

 string cn = "Jeremy Stafford";
 string group = "IT";
 string ou = "QGT";
 string domain1 = "QGT";
 string domain2 = "Local";

 string ldap = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", 
                             cn, group, ou, domain1, domain2);    
 string ldapFormatted = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", 
                                      cn, group, ou, domain1, domain2);                
 var ldapHardCoded = @"LDAP://CN=Jeremy Stafford, OU=IT, OU=QGT, DC=QGT, DC=Local";

 bool bothTheSame = ldapFormatted.Equals(ldapHardCoded) 
                    && 
                    ldapFormatted.GetType() == ldapHardCoded.GetType();

I believe somethign wrong with a hardcoded string, since if you copy hard coded values to the variables cn, group, ... you got working test. So please share a code which initializes a variables cn, group, ou, domain1, domain2.

Test code below works fine.

 string cn = "Jeremy Stafford";
 string group = "IT";
 string ou = "QGT";
 string domain1 = "QGT";
 string domain2 = "Local";

 string ldap = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", 
                             cn, group, ou, domain1, domain2);    
 string ldapFormatted = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", 
                                      cn, group, ou, domain1, domain2);                
 var ldapHardCoded = @"LDAP://CN=Jeremy Stafford, OU=IT, OU=QGT, DC=QGT, DC=Local";

 bool bothTheSame = ldapFormatted.Equals(ldapHardCoded) 
                    && 
                    ldapFormatted.GetType() == ldapHardCoded.GetType();
哑剧2025-01-14 15:08:44

嗯,为什么不尝试对 string.format 中的值进行硬编码,如果可行的话,说明您的变量有问题。

string ldap = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", "Name", "Department", "Group", "Domain1", "Domain2");

除此之外,我不认为为什么它不能与硬编码一起工作,而且没有的话,毕竟它们都是字符串。

hmm, why don't you try hard coding the values in string.format, if that works you have something wrong with your variables.

string ldap = string.Format("LDAP://CN={0}, OU={1}, OU={2}, DC={3}, DC={4}", "Name", "Department", "Group", "Domain1", "Domain2");

Other than that, I don't think why it wouldn't work with hard coding and without, they both are strings, afterall.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文