通过C#查询Active Directory [英] Qurey the Active Directory thru C#
问题描述
嗨Guru的
我写了下面的代码:
Hi Guru''s
Hi ,
I wrote a below code :
using System;
using System.Text;
using System.DirectoryServices;
namespace activeDirectoryLdapExamples
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter user: ");
String username = Console.ReadLine();
if (username.Contains("."))
{
string[] splitString = username.Split(new char[] { ''.'' });
username = splitString[splitString.Length - 1];
Console.WriteLine(username);
}
try
{
// create LDAP connection object
// DirectoryEntry myLdapConnection = createDirectoryEntry();
DirectoryEntry entry = new DirectoryEntry("GC://Test", "Test\\Test", "Ld@Test", AuthenticationTypes.Secure);
// create search object which operates on LDAP connection object
// and set search object to only find the user specified
// DirectorySearcher search = new DirectorySearcher(myLdapConnection);
System.DirectoryServices.DirectorySearcher search = new System.DirectoryServices.DirectorySearcher(entry);
// search.Filter = "(cn=" + username + ")";
search.Filter = "(sn=" + username + ")";
// create results objects from search object
SearchResult result = search.FindOne();
if (result != null)
{
// user exists, cycle through LDAP fields (cn, telephonenumber etc.)
ResultPropertyCollection fields = result.Properties;
foreach (String ldapField in fields.PropertyNames)
{
// cycle through objects in each field e.g. group membership
// (for many fields there will only be one object such as name)
foreach (Object myCollection in fields[ldapField])
Console.WriteLine(String.Format("{0,-20} : {1}", ldapField, myCollection.ToString()));
}
}
else
{
// user does not exist
Console.WriteLine("User not found!");
}
}
catch (Exception e)
{
Console.WriteLine("Exception caught:\n\n" + e.ToString());
}
}
}
}
在这种情况下,正在传递字符串(例如:will.smith),是使用IF语句获取姓氏(例如:smith),然后使用search.Filter =(sn =" + username +)";
现在我有一个问题,如果字符串来了(例如:Will(space)Smith),那么我将使用整个字符串并使用search.Filter =(cn =" + username +)";
我试图将if语句放入搜索过滤器中,但它不起作用,有人请给我一个主意.
[edit]添加了代码块-OriginalGriff [/edit]
Am passing the string (eg:will.smith) in that case am using IF statement to get the last name (eg:smith) and using search.Filter = "(sn=" + username + ")";
Now i have a problem,if the string comes like (eg: Will(space )Smith) then i will take the whole string and use the search.Filter = "(cn=" + username + ")";
i was trying to put if statement in search filter but it doesn''t work,some one please give me an idea.
[edit]Code Block added - OriginalGriff[/edit]
推荐答案
如果您确定总会有一个字符分隔名称,则可以使用以下代码段代替您的if子句.
If you are sure that there always will be one character separating the names you can use following snippet instead of your if clause.
char[] splitChars = new char[] {''.'', '' '', '','', '';'', '':'',...};
if (username.IndexOfAny(splitChars) != -1)
{
string[] splitString = username.Split(splitChars);
username = splitString[splitString.Length - 1];
Console.WriteLine(username);
}
否则,您应该尝试使用RegEx进行匹配.
Otherwise you should try to use RegEx to perform matching.
这篇关于通过C#查询Active Directory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!