字符串的DirectoryEntry工作为文字而不是格式化字符串? [英] DirectoryEntry string working as literal but not as formatted string?

查看:164
本文介绍了字符串的DirectoryEntry工作为文字而不是格式化字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我试图拉从Active Directory用户到DirectoryUser对象,如果我输入像这样,它工作正常:

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?!

编辑: 我只是想补充一点,我在几个不同的方式双重检查,以验证内置字符串是出来等同于硬codeD字符串,它是。我跑通过调试和检查user.path值以验证串在被存储完全相同。一切至今是相同的,但一期工程和其他不!

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!

更新: 我注意到,如果我硬$ C $直接℃到一个字符串变量:

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?

更新: 我跑了语义测试。这里是code我用:

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);

这是结果:

推荐答案

我想你的code和填充格式变量,它为我工作,你可以在截图中看到。

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);

您真的应该在你的输入变量检查什么。也许你有它的一些不可见的字符。您的输入变量转换为字节数组,并打印出数组以得到更深入的了解你有什么在里面。

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.

这篇关于字符串的DirectoryEntry工作为文字而不是格式化字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆