ASP.net and GAC

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();

}
 
K

Kevin Spencer

I have a car. What can you tell me about this car? You have an error. What
can I tell you about this error?

Not much. Could you describe the error, please? what is the type of
exception? What is the message? What does the stack trace in the exception
tell you about where the exception occurred?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

SP said:
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();

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top