S
SP
Hello there,
I have a following peice of code which access AD.
When I place the code in ASP.net page it works fine.
but when I create a class and put the code in the class, and then
compile the class and register it in the GAC, and reference it in my
ASP.net project,and create a instance of the class and call the method
which holds the method, I get an error.
below is the code, does the security context change when the class is
being called?, I have set impersonisation to true in the web
application
Please suggest!
Thanks,
SP
public string itnMembersOf()
{
string tmp1 = "";
DirectoryEntry my_ent = new
DirectoryEntry("LDAP://immunetolerance.org/OU=testou1,OU=testou2,DC=immunetolerance,DC=org");
my_ent.Username = "(e-mail address removed)";
my_ent.Password = "pass";
DirectorySearcher my_srch = new DirectorySearcher(my_ent);
my_srch.Filter = "(SAMAccountName=spai)";
SearchResult my_result = my_srch.FindOne () ;
DirectoryEntry my_ent1 = my_result.GetDirectoryEntry();
my_ent1.Username = "(e-mail address removed)";
my_ent1.Password = "pass";
my_ent1.RefreshCache(new string[] {"tokenGroups"});
foreach (object groupSid in my_ent1.Properties["tokenGroups"])
{
byte[] sid = (byte[])groupSid;
try
{
DirectoryEntry groupEntry = new
DirectoryEntry(string.Format("LDAP://<sid={0}>",
ConvertToOctetString(sid)));
groupEntry.Username ="(e-mail address removed)";
groupEntry.Password ="pass";
PropertyCollection propcoll = groupEntry.Properties;
string delimStr = ",";
char [] delimiter = delimStr.ToCharArray();
string[] atemp = null;
foreach(string key in propcoll.PropertyNames)
{
foreach(object values in propcoll[key])
{
if (key.ToLower() == "distinguishedname")
{
string temp;
temp = values.ToString();
if (temp.IndexOf("ImportedExchange") < 0)
{
atemp = temp.Split(delimiter);
tmp1 += atemp[0].Replace("CN=",",");
if (tmp1.StartsWith(","))
{
tmp1 = tmp1.Substring(1,tmp1.Length-2 );
}
}
}
}
}
}
catch(Exception ex)
{
TextBox1.Text = ex.Message ;
}
}
return tmp1;
public static string ConvertToOctetString(byte[] values)
{
return ConvertToOctetString(values,false,false);
}
public static string ConvertToOctetString(byte[] values, bool
isAddBackslash)
{
return ConvertToOctetString(values,isAddBackslash, false);
}
public static string ConvertToOctetString( byte[] values, bool
isAddBackslash, bool isUpperCase)
{
int iterator;
StringBuilder builder = new StringBuilder(values.Length * 2);
string slash;
if (isAddBackslash)
{
slash = "\\";
}
else
{
slash = string.Empty;
}
string formatCode;
if (isUpperCase)
{
formatCode = "X2";
}
else
{
formatCode = "x2";
}
for (iterator=0; iterator < values.Length; iterator++)
{
builder.Append(slash);
builder.Append(values[iterator].ToString(formatCode));
}
return builder.ToString();
}
I have a following peice of code which access AD.
When I place the code in ASP.net page it works fine.
but when I create a class and put the code in the class, and then
compile the class and register it in the GAC, and reference it in my
ASP.net project,and create a instance of the class and call the method
which holds the method, I get an error.
below is the code, does the security context change when the class is
being called?, I have set impersonisation to true in the web
application
Please suggest!
Thanks,
SP
public string itnMembersOf()
{
string tmp1 = "";
DirectoryEntry my_ent = new
DirectoryEntry("LDAP://immunetolerance.org/OU=testou1,OU=testou2,DC=immunetolerance,DC=org");
my_ent.Username = "(e-mail address removed)";
my_ent.Password = "pass";
DirectorySearcher my_srch = new DirectorySearcher(my_ent);
my_srch.Filter = "(SAMAccountName=spai)";
SearchResult my_result = my_srch.FindOne () ;
DirectoryEntry my_ent1 = my_result.GetDirectoryEntry();
my_ent1.Username = "(e-mail address removed)";
my_ent1.Password = "pass";
my_ent1.RefreshCache(new string[] {"tokenGroups"});
foreach (object groupSid in my_ent1.Properties["tokenGroups"])
{
byte[] sid = (byte[])groupSid;
try
{
DirectoryEntry groupEntry = new
DirectoryEntry(string.Format("LDAP://<sid={0}>",
ConvertToOctetString(sid)));
groupEntry.Username ="(e-mail address removed)";
groupEntry.Password ="pass";
PropertyCollection propcoll = groupEntry.Properties;
string delimStr = ",";
char [] delimiter = delimStr.ToCharArray();
string[] atemp = null;
foreach(string key in propcoll.PropertyNames)
{
foreach(object values in propcoll[key])
{
if (key.ToLower() == "distinguishedname")
{
string temp;
temp = values.ToString();
if (temp.IndexOf("ImportedExchange") < 0)
{
atemp = temp.Split(delimiter);
tmp1 += atemp[0].Replace("CN=",",");
if (tmp1.StartsWith(","))
{
tmp1 = tmp1.Substring(1,tmp1.Length-2 );
}
}
}
}
}
}
catch(Exception ex)
{
TextBox1.Text = ex.Message ;
}
}
return tmp1;
public static string ConvertToOctetString(byte[] values)
{
return ConvertToOctetString(values,false,false);
}
public static string ConvertToOctetString(byte[] values, bool
isAddBackslash)
{
return ConvertToOctetString(values,isAddBackslash, false);
}
public static string ConvertToOctetString( byte[] values, bool
isAddBackslash, bool isUpperCase)
{
int iterator;
StringBuilder builder = new StringBuilder(values.Length * 2);
string slash;
if (isAddBackslash)
{
slash = "\\";
}
else
{
slash = string.Empty;
}
string formatCode;
if (isUpperCase)
{
formatCode = "X2";
}
else
{
formatCode = "x2";
}
for (iterator=0; iterator < values.Length; iterator++)
{
builder.Append(slash);
builder.Append(values[iterator].ToString(formatCode));
}
return builder.ToString();
}