Active Directory ile ilgilenenler için güzel bir class. Daha önce vermiş olduğum dns server yönetim class ı ile birlikte güzel şeyler yapılabilir ;)
Bu classın size göre eksikleri olabilir.
Sorunlarınızı bize iletirseniz beraber çözüm de bulabiliriz.
public class ADHelper
{
#region Private Variables
private static string ADServer = ConfigurationManager.AppSettings["ADServer"].ToString();private static string ADFullPath = ConfigurationManager.AppSettings["ADFullPath"].ToString();
private static string ADUser = ConfigurationManager.AppSettings["ADAdminUser"].ToString();
private static string ADPassword = ConfigurationManager.AppSettings["ADAdminPassword"].ToString(); private static string ADPath = ConfigurationManager.AppSettings["ADPath"].ToString();
#endregion
public static bool UserExists(string UserName)
{
//create an instance of the DirectoryEntry
DirectoryEntry de = GetDirectoryObject();
//create instance fo the direcory searcher
DirectorySearcher deSearch = new DirectorySearcher();
//set the search filter
deSearch.SearchRoot =de;
deSearch.Filter = "(&(objectClass=user) (cn=" + UserName +"))";
//find the first instance
SearchResultCollection results= deSearch.FindAll();
//if the username and password do match, then this implies a valid login
//if so then return the DirectoryEntry object
if(results.Count ==0)
{
return false;
}
else
{
return true;
}
}
public static DataSet GetGroups()
{
DataSet dsGroup = new DataSet();
DirectoryEntry de = GetDirectoryObject();
//create instance fo the direcory searcher
DirectorySearcher deSearch = new DirectorySearcher();
//set the search filter
deSearch.SearchRoot =de;
deSearch.PropertiesToLoad.Add("cn"); deSearch.Filter = "(&(objectClass=group)(cn=*))";
//find the first instance
SearchResultCollection results= deSearch.FindAll();
//Create a new table object within the dataset
DataTable tbGroup = dsGroup.Tables.Add("Groups"); tbGroup.Columns.Add("GroupName");
//if there are results (there should be some!!), then convert the results
//into a dataset to be returned.
if(results.Count >0)
{
//iterate through collection and populate the table with
//the Group Name
foreach (SearchResult Result in results)
{
//set a new empty row
DataRow rwGroup = tbGroup.NewRow();
//populate the column
rwGroup["GroupName"]= Result.Properties["cn"][0];
//append the row to the table of the dataset
tbGroup.Rows.Add(rwGroup);
}
}
return dsGroup;
}
public static bool UserIsMemberOfGroup(string GroupName,string UserName)
{
bool IsMemberOf = false;
DirectoryEntry de = GetDirectoryObject();
//create instance fo the direcory searcher
DirectorySearcher deSearch = new DirectorySearcher();
//set the search filter
deSearch.SearchRoot =de;
//deSearch.PropertiesToLoad.Add("cn");
deSearch.Filter = "(&(objectClass=group)(cn=" + GroupName +"))";
//get the group result
SearchResult results= deSearch.FindOne();
//if the group is valid, then continue, otherwise return a blank dataset
if (results != null)
{ //create a link to the group object, so we can get the list of members
DirectoryEntry deGroup = new DirectoryEntry(results.Path, ADUser, ADPassword, AuthenticationTypes.Secure);
//assign a property collection
System.DirectoryServices.PropertyCollection pcoll = deGroup.Properties;
int n = pcoll["member"].Count;
//if there are members fo the group, then get the details and assign to the table
for (int l = 0; l < n; l++)
{ //create a link to the user object sot hat the FirstName, LastName and SUername can be gotten
DirectoryEntry deUser = new DirectoryEntry("LDAP://" + pcoll["member"][l].ToString(), ADUser, ADPassword, AuthenticationTypes.Secure);
//set a new empty row
DirectoryEntry deGroupUsers = GetDirectoryObject(); DirectorySearcher deUserSearch = new DirectorySearcher();
deUserSearch.SearchRoot = deUser;
deUserSearch.Filter = "(&(objectClass=user)(cn=" + "John Rieder" + "))";
//SearchResultCollection userResults = deUserSearch.FindAll();
SearchResult userResult = deUserSearch.FindOne();
string UserDisplayName = GetProperty(deUser, "cn");
if (UserDisplayName == UserName)
{
IsMemberOf = true; break;
}
else
{
IsMemberOf = false;
}
}
}
return IsMemberOf;
}
public static void AddUserToGroup(string UserName, string GroupName)
{
string LDAPDomain = string.Empty;
LDAPDomain = "CN=" + GroupName + ",CN=Users," + GetLDAPDomain();
DirectoryEntry deGroup = GetDirectoryObject(LDAPDomain);
LDAPDomain = "CN=" + UserName + ",CN=Users," + GetLDAPDomain();
DirectoryEntry deUser = GetDirectoryObject(LDAPDomain);
//deSearchGroup.Filter = "(&(objectClass=group)(cn=" + GroupName + "))";
//deSearchGroup.Filter = "(&(objectClass=user)(cn=" + UserName + "))";
deGroup.Invoke("Add", new Object[] { deUser.Path.ToString() });
deGroup.Close();
deUser.Close();
}
public static void RemoveUserFromGroup(string UserName, string GroupName)
{
string LDAPDomain = string.Empty;
LDAPDomain = "CN=" + GroupName + ",CN=Users," + GetLDAPDomain();
DirectoryEntry deGroup = GetDirectoryObject(LDAPDomain);
LDAPDomain = "CN=" + UserName + ",CN=Users," + GetLDAPDomain();
DirectoryEntry deUser = GetDirectoryObject(LDAPDomain);
//deSearchGroup.Filter = "(&(objectClass=group)(cn=" + GroupName + "))";
//deSearchGroup.Filter = "(&(objectClass=user)(cn=" + UserName + "))";
deGroup.Invoke("Remove", new Object[] { deUser.Path.ToString() });
deGroup.Close();
deUser.Close();
}
#endregion
Mustafa KOÇER
mkocer@dotnetkosesi.com
2 kişi tarafından 5.0 olarak değerlendirildi
- Currently 5/5 Stars.
- 1
- 2
- 3
- 4
- 5