R
Robert Wallström
Hi
I am trying to add a user to a group in Active Directory using
System.Directory
Services
But when I CommitChanges() I get the following exeption:
(In swedish, I use a swedish version of XP-pro)
"Egenskapen TxIsolationLevel för den COM+-komponent som skapas är starkare
är TxIsolationLevel för transaktionens rotkomponent. Objektet kunde inte
skapas."
Freely interpreted to English:
"The property TxIsolationLevel for the COM+-component that is being created
is stronger than
TxIsolationLevel for the transaktions rootcomponent. The object could not be
created."
My code:
public class AdManipulator
{
private DirectoryEntry root;
private DirectorySearcher adSearcher;
private string topDomain;
private string domain;
private string manipulatorName;
private string manipulatorPass;
private string path;
public AdManipulator(string newManipulatorName, string
newManipulatorPass,string newAdDomain)
{
topDomain = newAdDomain.Substring(newAdDomain.IndexOf(".") + 1);
domain = newAdDomain.Substring(0,newAdDomain.IndexOf("."));
path = "LDAP://DC=" + domain + ",DC=" + topDomain;
manipulatorName = newManipulatorName;
manipulatorPass = newManipulatorPass;
root = new DirectoryEntry();
root.Username = newManipulatorName;
root.Password = newManipulatorPass;
root.Path = path;
root.AuthenticationType = AuthenticationTypes.Secure;
adSearcher = new DirectorySearcher(root);
}
//Below is the method wich casts exeption...
public bool addUserToGroup(AdUser user)
{
try
{
adSearcher.Filter = "(sAMAccountName=" + user.Username + ")";
SearchResult res = adSearcher.FindOne();
if(res == null)
{
throw new Exception("Error no such user!\n");
}
DirectoryEntry deUser = new DirectoryEntry(res.Path);
foreach(string st in user.Groups)
{
adSearcher.Filter = "(CN=" + st + ")";
res = adSearcher.FindOne();
if(res != null)
{
DirectoryEntry group = new DirectoryEntry(res.Path);
group.Properties["member"].Add(deUser.Properties["distinguishedName"].Value)
;
group.CommitChanges();//on executing this row I get an exeption...
}
}
}
catch(Exception ex)
{
throw new Exception("Error adding user to group.\n" + ex.Message);
}
return true;
}
}
//Bellow is the classhead for the AdUser object this is just a
//object wich carries data about a specific user..
//this object is used in addUserToGroup(AdUser user)
public class AdUser
{
//Common user variables, more could be used..
private string username;
private string password;
private string givenname;
private string initials;
private string surname;
private string displayname;
private string discription;
private string telephoneNumber;
private string mail;
private string url;
private StringCollection groups = new StringCollection();
}
Have anyone got an similar exeption?
Or might anyone se what Im doing wrong in my code..
Thank you...
I am trying to add a user to a group in Active Directory using
System.Directory
Services
But when I CommitChanges() I get the following exeption:
(In swedish, I use a swedish version of XP-pro)
"Egenskapen TxIsolationLevel för den COM+-komponent som skapas är starkare
är TxIsolationLevel för transaktionens rotkomponent. Objektet kunde inte
skapas."
Freely interpreted to English:
"The property TxIsolationLevel for the COM+-component that is being created
is stronger than
TxIsolationLevel for the transaktions rootcomponent. The object could not be
created."
My code:
public class AdManipulator
{
private DirectoryEntry root;
private DirectorySearcher adSearcher;
private string topDomain;
private string domain;
private string manipulatorName;
private string manipulatorPass;
private string path;
public AdManipulator(string newManipulatorName, string
newManipulatorPass,string newAdDomain)
{
topDomain = newAdDomain.Substring(newAdDomain.IndexOf(".") + 1);
domain = newAdDomain.Substring(0,newAdDomain.IndexOf("."));
path = "LDAP://DC=" + domain + ",DC=" + topDomain;
manipulatorName = newManipulatorName;
manipulatorPass = newManipulatorPass;
root = new DirectoryEntry();
root.Username = newManipulatorName;
root.Password = newManipulatorPass;
root.Path = path;
root.AuthenticationType = AuthenticationTypes.Secure;
adSearcher = new DirectorySearcher(root);
}
//Below is the method wich casts exeption...
public bool addUserToGroup(AdUser user)
{
try
{
adSearcher.Filter = "(sAMAccountName=" + user.Username + ")";
SearchResult res = adSearcher.FindOne();
if(res == null)
{
throw new Exception("Error no such user!\n");
}
DirectoryEntry deUser = new DirectoryEntry(res.Path);
foreach(string st in user.Groups)
{
adSearcher.Filter = "(CN=" + st + ")";
res = adSearcher.FindOne();
if(res != null)
{
DirectoryEntry group = new DirectoryEntry(res.Path);
group.Properties["member"].Add(deUser.Properties["distinguishedName"].Value)
;
group.CommitChanges();//on executing this row I get an exeption...
}
}
}
catch(Exception ex)
{
throw new Exception("Error adding user to group.\n" + ex.Message);
}
return true;
}
}
//Bellow is the classhead for the AdUser object this is just a
//object wich carries data about a specific user..
//this object is used in addUserToGroup(AdUser user)
public class AdUser
{
//Common user variables, more could be used..
private string username;
private string password;
private string givenname;
private string initials;
private string surname;
private string displayname;
private string discription;
private string telephoneNumber;
private string mail;
private string url;
private StringCollection groups = new StringCollection();
}
Have anyone got an similar exeption?
Or might anyone se what Im doing wrong in my code..
Thank you...