This segment of code finds a given object based on search criteria. Based on whether you put into the command line either user or machine, it'll allow you to display very LDAP properties of such objects, including the AD path. Please view below:
public void SearchObject(string server, string name1, string obj1)
{
try
{
this.Label1.Style["text-align"] = "left";
Label1.ForeColor = Color.Black;
DirectoryEntry root1 = new DirectoryEntry(@"LDAP://rootdse");
string path1 = root1.Invoke("GET", "defaultNamingContext").ToString();
DirectoryEntry root = new DirectoryEntry(
"LDAP://" + path1, txtUsername.Text, txtPassword.Text); //OU=Computers,OU=Home,dc=ds,dc=apollo,dc=com
DirectorySearcher searcher = new DirectorySearcher(root);
if (name1 == "name" || name1 == "person" || name1 == "user")
{
searcher.Filter = "(&(objectClass=*)(sn=" + obj1 + "))";
searcher.PropertiesToLoad.Add("cn");
searcher.PropertiesToLoad.Add("userPrincipalName");
searcher.PropertiesToLoad.Add("sAMAccountName");
searcher.PropertiesToLoad.Add("displayname");
searcher.PropertiesToLoad.Add("description");
searcher.PropertiesToLoad.Add("department");
searcher.PropertiesToLoad.Add("manager");
searcher.PropertiesToLoad.Add("homephone");
searcher.PropertiesToLoad.Add("mobile");
searcher.PropertiesToLoad.Add("location");
searcher.PropertiesToLoad.Add("streetaddress");
searcher.PropertiesToLoad.Add("mail");
searcher.PropertiesToLoad.Add("telephonenumber");
}
else
{
searcher.Filter = "(&(objectClass=*)(cn=" + obj1 + "))";
searcher.PropertiesToLoad.Add("cn");
searcher.PropertiesToLoad.Add("userPrincipalName");
searcher.PropertiesToLoad.Add("sAMAccountName");
searcher.PropertiesToLoad.Add("displayname");
searcher.PropertiesToLoad.Add("description");
searcher.PropertiesToLoad.Add("department");
searcher.PropertiesToLoad.Add("manager");
searcher.PropertiesToLoad.Add("mail");
}
Label1.Text = "";
Label1.Text += "---------------------------------------------------------------------------- <br />";
SearchResultCollection results = searcher.FindAll();
foreach (SearchResult result in results)
{
string searchpath = result.Path;
Console.WriteLine("path: {0}", searchpath);
// Label1.Text += "path: <b>" + searchpath + "</b><br /><br />";
ResultPropertyCollection rpc = result.Properties;
foreach (string property in rpc.PropertyNames)
{
foreach (object value in rpc[property])
{
if (property == "mail")
{
Label1.Text += "<b>" + property +
"</b>: <a href='mailto:" + value + "'>" + value + "</a><br />";
}
else
{
Label1.Text += "<b>" + property +
"</b>: " + value + "<br />";
}
}
}
Label1.Text += "---------------------------------------------------------------------------- <br />";
}
}
catch (Exception ex)
{
Label1.Text += ex.Message;
}
}
Various Scripts and Application Code Segments for .NET, VB, C#, C++, C, Java, JavaScript, HTML, Python, Perl, AutoIT, Batch, ASP Classic, Objective-C, Swift, Unreal Engine 4, Unity3D & others. Also contains numerous IT tidbits, procedures, and tricks including Technology Hacks on various platforms.
Subscribe to:
Post Comments (Atom)
Generating "Always On Top" NSWindow in macOS across all detected displays
Also: Using UIKit & Cocoa Frameworks using Objective-C In m acOS or OS X , written in either Objective-C or Swift Langues, you m...
-
In Unreal Engine 4 you will eventually need Linear Interpolation between two values to do something like ping pong between two float val...
-
Recently Possess () has been deprecated from UE4 , and when writing classes based on AAIController you have to use the function OnPossess ...
-
Often we intermingle C++ and Blueprints, and need for the two to communicate. With Behavior Trees, using ENUMs is an everyday occurrence an...
No comments:
Post a Comment