Hi,
Here you go:
Validation for a single user to the active directory
public static bool User()
{
bool validation;
try
{
LdapConnection lcon = new LdapConnection(new LdapDirectoryIdentifier((string)null, false, false));
NetworkCredential nc = new NetworkCredential(Environment.UserName, "kals123", Environment.UserDomainName);
lcon.Credential = nc;
lcon.AuthType = AuthType.Negotiate;
lcon.Bind(nc);
validation = true;
}
catch (LdapException)
{
validation = false;
}
return validation;
}
Lists all the users from current domain
public static void getUser()
{
DirectoryEntry directoryEntry = new DirectoryEntry("WinNT://" + Environment.UserDomainName);
string userNames = "";
string authenticationType="";
foreach (DirectoryEntry child in directoryEntry.Children)
{
if (child.SchemaClassName == "User")
{
userNames += child.Name + Environment.NewLine;
authenticationType += child.Username + Environment.NewLine;
}
}
Console.WriteLine("************************Users************************");
Console.WriteLine(userNames);
Console.WriteLine("*****************Authentication Type*****************");
}
Getting user names with groups
public static void fnGetListOfUsers() {
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "USERS");
if (group != null)
{
foreach (Principal p in group.GetMembers())
{
Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName);
}
}
}
Getting a particular user details from user's active directory
public static void fnImp() {
using (var context = new PrincipalContext(ContextType.Domain, Environment.UserDomainName))
{
using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
{
foreach (var result in searcher.FindAll())
{
DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
if ((string)de.Properties["givenName"].Value == Environment.UserName)
{
Console.WriteLine();
PropertyCollection pc = de.Properties;
foreach (PropertyValueCollection col in pc)
{
Console.WriteLine(col.PropertyName + " : " + col.Value);
Console.WriteLine();
}
}
}
}
}
Console.ReadLine();
}
What you need is all here.
--Amit