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.Web.Administration;
using System.Net;
class AD
{
//private static DirectoryEntry User = GetDirectoryEntry();
//private string
//private string
//private string
private string AD_DisplayName = "";
private string AD_SAMAccountName = "";
private string AD_givenname = "";
private string AD_mail = "";
private string AD_sn = "";
private string AD_initials = "";
private Boolean AD_userAccountControl = true;
private string AD_domain = "@";
private string AD_password = "";
private string AD_new_password = "";
private string AD_User = "";
private static string AD_Admin_User = "";
private static string AD_Admin_Host = "";
private static string AD_Admin_pwd = "";
//private static DirectoryEntry AD_UserOb;
//***********************************************
private enum ADAccountOptions
{
UF_TEMP_DUPLICATE_ACCOUNT = 0x0100,
UF_NORMAL_ACCOUNT = 0x0200,
UF_INTERDOMAIN_TRUST_ACCOUNT = 0x0800,
UF_WORKSTATION_TRUST_ACCOUNT = 0x1000,
UF_SERVER_TRUST_ACCOUNT = 0x2000,
UF_DONT_EXPIRE_PASSWD = 0x10000,
UF_SCRIPT = 0x0001,
UF_ACCOUNT_DISABLE = 0x0002,
UF_HOMEDIR_REQUIRED = 0x0008,
UF_LOCKOUT = 0x0010,
UF_PASSWD_NOTREQD = 0x0020,
UF_PASSWD_CANT_CHANGE = 0x0040,
UF_ACCOUNT_LOCKOUT = 0X0010,
UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED = 0X0080,
}
private string KillCN(string CN)
{
return CN.Substring(3, CN.Length - 3);
}
private string GetNameFromPath(string CN)
{
string s1 = "";
int i = 3;
while (CN[i] != ',')
{
s1 += CN[i];
i += 1;
}
return s1;
}
public string[] GroupGetAll()
{
DirectoryEntry Group = GetDirectoryEntry();
String[] AllGroups ={ };
int pos = 0;
foreach (DirectoryEntry group in Group.Children)
{
try
{
int t = (int)group.Properties["grouptype"].Value; //if (t) //0x80000002 2147483650
Array.Resize(ref AllGroups, AllGroups.Length + 1);
AllGroups[pos] = KillCN(group.Name.ToString());
pos += 1;
}
catch (Exception ex)
{
//Response.Write(ex.Message);
}
}
return AllGroups;
}
public string[] GroupForUsers(string GroupName)
{
DirectoryEntry GR = ObjAD("group", GroupName, true);
String[] AllGroups ={ };
int pos = 0;
foreach (string member in GR.Properties["member"])
{
Array.Resize(ref AllGroups, AllGroups.Length + 1);
AllGroups[pos] = GetNameFromPath(member);//KillCN(group.Name.ToString());
pos += 1;
}
return AllGroups;
}
public string[] UserOfGroups(string UserName)
{
DirectoryEntry Group = GetDirectoryEntry();
String[] AllGroups ={ };
int pos = 0;
foreach (DirectoryEntry group in Group.Children)
{
try
{
int t = (int)group.Properties["grouptype"].Value; //if (t) //0x80000002 2147483650
foreach (string member in group.Properties["member"])
{
if (member.Contains(UserName))
{
Array.Resize(ref AllGroups, AllGroups.Length + 1);
AllGroups[pos] = KillCN(group.Name.ToString());
pos += 1;
}
}
}
catch (Exception ex)
{
//Response.Write(ex.Message);
}
}
return AllGroups;
}
public string[] UsersGetAll()
{
DirectoryEntry Users = GetDirectoryEntry();
String[] AllUsers ={ };
int pos = 0;
foreach (DirectoryEntry user in Users.Children)
{
try
{
int t = (int)user.Properties["grouptype"].Value; //if (t) //0x80000002 2147483650
}
catch (Exception ex)
{
Array.Resize(ref AllUsers, AllUsers.Length + 1);
AllUsers[pos] = KillCN(user.Name.ToString());
pos += 1;
//Response.Write(ex.Message);
}
}
return AllUsers;
}
public void UserRemove(string UserName)
{
try
{
Roles.RemoveUserFromRoles(UserName, Roles.GetRolesForUser(UserName));
}
catch (Exception ex)
{
// Err(ex);
}
finally
{
DirectoryEntry UR = ObjAD("user", UserName, true);
DirectoryEntry del = UR.Parent;
del.Children.Remove(UR);
del.CommitChanges();
del.Close();
}
}
public void GroupRemove(string GroupName)
{
DirectoryEntry GR = ObjAD("group", GroupName, true);
DirectoryEntry del = GR.Parent;
del.Children.Remove(GR);
del.CommitChanges();
del.Close();
}
public static DirectoryEntry GetDirectoryEntry()
{
DirectoryEntry DR = new DirectoryEntry();
DR.Path = AD_Admin_Host; //"LDAP://ANDRUX.MY.LOC/CN=Users,DC=MY,DC=LOC";
DR.Username = AD_Admin_User;// @"domain\AndruX";
DR.Password = AD_Admin_pwd; //"********";
return DR;
}
private static DirectoryEntry ObjAD(string type, string name, Boolean exist)
{
DirectoryEntry DR = GetDirectoryEntry();
DirectoryEntry User;
if (exist)
{
User = DR.Children.Find("CN=" + name, type);
}
else
{
User = DR.Children.Add("CN=" + name, type);
}
DR.Close();
//ObjAD = User;
return User;
}
public void UserSave()
{
DirectoryEntry User = ObjAD("user", AD_User, false);
User.CommitChanges();
if (AD_userAccountControl)
{
User.Properties["userAccountControl"].Value = ADAccountOptions.UF_NORMAL_ACCOUNT;// val & ~0x2;// ~0x1 не активный
}
else
{
User.Properties["userAccountControl"].Value = ADAccountOptions.UF_ACCOUNT_DISABLE;// ~0x1 не активный
}
User.CommitChanges();
User.Invoke("Put", new object[] { "userPrincipalName", AD_SAMAccountName + "@" + AD_domain });
User.CommitChanges();
User.Properties["DisplayName"].Value = AD_DisplayName;
User.Properties["SAMAccountName"].Value = AD_SAMAccountName;
User.Invoke("ChangePassword", new object[] { AD_password, AD_new_password });
// DR.Properties["userpassword"].Value = AD_password;
User.CommitChanges();
User.Properties["givenname"].Value = AD_givenname;
User.Properties["mail"].Value = AD_mail;
User.Properties["sn"].Value = AD_sn; //lastname
User.Properties["initials"].Value = AD_initials; // initials
User.CommitChanges();
}
public void UserCreateNew(string UserName, string Password)
{
AD_User = UserName;
AD_password = Password;
AD_new_password = Password;
AD_SAMAccountName = UserName;
AD_DisplayName = UserName;
AD_givenname = UserName;
}
public void UserGet(string UserName)
{
DirectoryEntry User = ObjAD("user", UserName, true);
User.CommitChanges();
/*if (AD_userAccountControl)
{
User.Properties["userAccountControl"].Value = ADAccountOptions.UF_NORMAL_ACCOUNT;// val & ~0x2;// ~0x1 не активный
}
else
{
User.Properties["userAccountControl"].Value = ADAccountOptions.UF_ACCOUNT_DISABLE;// ~0x1 не активный
}*/
AD_DisplayName = User.Properties["DisplayName"].Value.ToString();
AD_SAMAccountName = User.Properties["SAMAccountName"].Value.ToString();
AD_password = User.Properties["userpassword"].Value.ToString();
// User.Invoke("Put", new object[] { "userPrincipalName", AD_SAMAccountName + "@" + AD_domain });
AD_givenname = User.Properties["givenname"].Value.ToString();
AD_mail = User.Properties["mail"].Value.ToString();
AD_sn = User.Properties["sn"].Value.ToString();
AD_initials = User.Properties["initials"].Value.ToString();
User.Close();
}
public void GroupCreateNew(string GroupName)
{
DirectoryEntry GR = ObjAD("group", GroupName, false);
GR.CommitChanges();
GR.Close();
}
public void GroupAddUser(string GroupName, string UserName)
{
DirectoryEntry GR = ObjAD("group", GroupName, true);
DirectoryEntry UR = ObjAD("user", GroupName, true);
GR.Properties["member"].Add(UR.Properties["distinguishedName"].Value);
GR.CommitChanges();
UR.Close();
GR.Close();
}
public void GroupRemoveUser(string GroupName, string UserName)
{
DirectoryEntry GR = ObjAD("group", GroupName, true);
DirectoryEntry UR = ObjAD("user", UserName, true);
GR.Properties["member"].Remove(UR.Properties["distinguishedName"].Value);
GR.CommitChanges();
UR.Close();
GR.Close();
}
public string pUserName
{
get
{
return AD_User;
}
set
{
AD_User = value;
}
}
public string pDisplayName
{
get
{
return AD_DisplayName;
}
set
{
AD_DisplayName = value;
}
}
public string pSAMAccountName
{
get
{
return AD_SAMAccountName;
}
set
{
AD_SAMAccountName = value;
}
}
public string pGivenName
{
get
{
return AD_givenname;
}
set
{
AD_givenname = value;
}
}
public string pMail
{
get
{
return AD_mail;
}
set
{
AD_mail = value;
}
}
public string pSn
{
get
{
return AD_sn;
}
set
{
AD_sn = value;
}
}
public string pInitials
{
get
{
return AD_initials;
}
set
{
AD_initials = value;
}
}
public string pDomain
{
get
{
return AD_domain;
}
set
{
AD_domain = value;
}
}
public string pPassword
{
set
{
AD_new_password = value;
}
}
public string aHost
{
set
{
AD_Admin_Host = value;
}
}
public string aPassword
{
set
{
AD_Admin_pwd = value;
}
}
public string aSeperUser
{
set
{
AD_Admin_User = value;
}
}
protected void SetProperty(DirectoryEntry DR, string PropertyName, string PropertyValue)
{
if (PropertyValue != null)
{
if (DR.Properties.Contains(PropertyName))
{
DR.Properties[PropertyName][0] = PropertyValue;
}
else
{
DR.Properties[PropertyName].Add(PropertyValue);
}
}
}
protected string GetProperty(DirectoryEntry DR, string PropertyName)
{
if (PropertyName != null)
{
if (DR.Properties.Contains(PropertyName))
{
return DR.Properties[PropertyName][0].ToString();
}
else
{
return DR.Properties[PropertyName].ToString();
}
}
else
{
return "";
}
}
}