3. Validate if a user exists
///
/// Method to validate if a user exists in the AD.
///
///
///
public bool UserExists(string UserName){
DirectoryEntry de = ADHelper.GetDirectoryEntry();
DirectorySearcher deSearch = new DirectorySearcher();
deSearch.SearchRoot =de;
deSearch.Filter = "(&(objectClass=user) (cn=" + UserName +"))";
SearchResultCollection results = deSearch.FindAll();
if(results.Count == 0){
return false;
}
else{
return true;
}
}
In the Code is a Class ADHelper, but this Class dosen't exist. Is this Example not finished or what is the problem?
I really need this Script. I have a Project in my company. To say is, that i'm a Noobie. My question is maybe stupid, but i hope you can help me?
Thank you very much!!
Greetz Simon
SimonPosted Oct 28, 2006, 7:47 AM
How can i do this....???? please help me!
I searched the hole Web for this, but it doesn't work....
how can i get a user Object to change the properties of a user?? This is my last question, but i need this!
SimonPosted Oct 22, 2006, 10:53 AM
But which Object must I transfer? or how can i have the User as a directoryEntry, becauce in the class "SetProperty" I must transfer a Directory Entry Object..
Here ist the Code. It's a bit long, but when you include the code in your editor, is not that the problem.
I thank you very much for help!!
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.DirectoryServices;
using System.Globalization;
using System.Security.Principal;
using System.Web.Services.Description;
//using DirectoryUtility.DirectoryServices;
//namespace DirectoryUtility
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
GetDirectoryEntry();
UserExists(textbox_user.Text);
}
//1. Create a connection to Active Directory
///
/// Method used to create an entry to the AD.
/// Replace the path, username, and password.
///
///
public static DirectoryEntry GetDirectoryEntry()
{
DirectoryEntry de = new DirectoryEntry();
//de.Path = "LDAP://192.168.1.1/CN=WebIDA;DC=Main";
de.Path = "LDAP://sv01/OU=WebIDA,DC=Main,DC=local";
de.Username = @"Main\programmer";
de.Password = "123asd$";
de.AuthenticationType = AuthenticationTypes.Secure;
return de;
}
//2. Create a secure connection to Active Directory
///
/// Establish identity (principal) and culture for a thread.
///
/*public static void SetCultureAndIdentity()
{
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsPrincipal principal = (WindowsPrincipal)Thread.CurrentPrincipal;
WindowsIdentity identity = (WindowsIdentity)principal.Identity;
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
}*/
//3. Validate if a user exists
///
/// Method to validate if a user exists in the AD.
///
///
///
public bool UserExists(string UserName)
{
DirectoryEntry de = GetDirectoryEntry();
DirectorySearcher deSearch = new DirectorySearcher();
deSearch.SearchRoot = de;
deSearch.Filter = "(&(objectClass=user) (cn=" + UserName + "))";
SearchResultCollection results = deSearch.FindAll();
if (results.Count == 0)
{
//test_button.Visible = false;
Label1.Text = "User besteht nicht!";
return false;
}
else
{
//List all AD-account Properties
PopulateProperties(de);
//test_button.Visible = true;
Label1.Text = "User besteht";
return true;
}
}
//4. Set user's properties
///
/// Helper method that sets properties for AD users.
///
///
///
///
public static void SetProperty(DirectoryEntry de, string PropertyName, string PropertyValue)
{
if (PropertyValue != null)
{
if (de.Properties.Contains(PropertyName))
{
de.Properties[PropertyName][0] = PropertyValue;
}
else
{
de.Properties[PropertyName].Add(PropertyValue);
}
}
}
//gibt die properties des accounts userenty aus
public void PopulateProperties(DirectoryEntry userEntry)
{
if (userEntry != null)
{
//propertiesListBox.clear();
foreach (string propertyName in userEntry.Properties.PropertyNames)
{
//string propertyName = userEntry.Password.ToString;
propertiesListBox.Items.Add(propertyName);
//Property Ausgabe (Listbox)
PropertyOutput(userEntry, propertyName);
}
}
}
//gibt den Wert der Properties ind PopulateProperties aus
public void PropertyOutput(DirectoryEntry de, string propertyName)
{
//string loginID = de.Properties["sAMAccountName"].Value.ToString();
string strPropertie = de.Properties[propertyName].Value.ToString();
//MessageBox.Show("Login ID: " + loginID.ToString() + "\r\n Full Name " + firstName.ToString(), "Results");
ListBox1.Items.Add(strPropertie);
}
}
Mark RansomPosted Oct 11, 2006, 10:14 AM
Later
Mark
SimonPosted Oct 11, 2006, 8:36 AM
objectClass
ou
distinguishedName
nstanceType
whenCreated
whenChanged
name
objectGUID
objectCategory
nTSecurityDescritor
Where are the properties from the Account like accountname, first-, lastname, street, country, etc. ?
Anybody knows? Thank you very much!
Mark RansomPosted Oct 10, 2006, 10:01 PM
http://geekswithblogs.net/mhamilton/archive/2005/10/04/55920.aspx
Especially pay attention to the bottom part where he explains how to list all of the properties available in your directory.
Good luck.
Mark
SimonPosted Oct 10, 2006, 10:43 AM
it works very good!
Anyway, do you know a list, how can i speak to a user account properties.
For examle: when i will change the Street or the e-mail or office or the notes etc.??
Mark RansomPosted Oct 9, 2006, 9:52 PM
public static DirectoryEntry GetDirectoryEntry()
{
DirectoryEntry de = new DirectoryEntry();
de.Path = "LDAP://192.168.1.1/CN=Users;DC=Yourdomain";
de.AuthenticationType = AuthenticationTypes.Secure;
return de;
}
public bool UserExists(string UserName)
{
DirectoryEntry de = GetDirectoryEntry();
DirectorySearcher deSearch = new DirectorySearcher();
deSearch.SearchRoot =de;
deSearch.Filter = "(&(objectClass=user) (cn=" + UserName +"))";
SearchResultCollection results = deSearch.FindAll();
if(results.Count == 0)
{
return false;
}
else
{
return true;
}
}
The GetDirectoryEntry() method is declared in examples one and two (one explicitly sets the authentication and the other uses the logged on user to authenticate), so you can just use one of those methods (whichever is appropriate for you) to get a root entry.
Later
Mark