error: {System.Runtime.InteropServices.COMException} occured

I

iusfani

Hi,

I am trying to connect with Active Directory & Active Directory
Application Mode(AD/AM), and want to pull down user's information, but
unfortunately I am encountering an error, which is like:

<error: an exception of type:
{{System.Runtime.InteropServices.COMException} occured>

Actually what I am trying to do is, I have designed a very simple
ASP.NET page, I am passing userID from there and want to get the user's
profile from AD and/or AD/AM. The code for getting user's information
is:

========================Code Snippet============================
private void getUserData(string UserID)
{
DirectoryEntry de = new DirectoryEntry();
DirectorySearcher aSearcher = new DirectorySearcher(de);
StringBuilder filter = new StringBuilder();
filter.AppendFormat("(anr = {0})", UserID);
aSearcher.Filter = filter.ToString();
SearchResult searchResult = aSearcher.FindOne();

if(searchResult != null){
DirectoryEntry directoryObject = searchResult.GetDirectoryEntry();
if(directoryObject.Properties["name"].Value != null)
name = (string)directoryObject.Properties["name"].Value;

if(directoryObject.Properties["regionID"].Value != null)
this.regionID =
Int32.Parse(directoryObject.Properties["regionID"].Value.ToString());
else
this.regionID = 20;
if (directoryObject.Properties["districtID"].Value!=null)
this.districtID =
Int32.Parse(directoryObject.Properties["districtID"].Value.ToString());
else
this.districtID=20;
if (directoryObject.Properties["homePhone"].Value !=null)
this.claimsOfficerID =
Int32.Parse(directoryObject.Properties["claimsOfficerID"].Value.ToString());
else
this.claimsOfficerID=-1;
}
else
{
throw new NullReferenceException("No such directory entry exists");
}
==========================End Code===============================

Now, When I call getUserData(...) method it throws an error "<error:
{System.Runtime.InteropServices.COMException} occured>"

Could any one please let me know why is that so?

Would be highly appreciated.

Thanks,

Inam
 
N

Nicholas Paldino [.NET/C# MVP]

iusfani,

The problem probably comes from the fact that it is running under the
ASPNET user, which doesn't have any rights to access AD. In order to get
around this, you will have to run the page under a user that has rights to
access it (either by using P/Invoke and the WindowsImpersonationContext
class, or by setting the impersonation in web.config).

I believe that the objects for accessing AD have a way of setting this
information, but I'm not sure what it is. You might want to snoop around
the System.DirectoryServices namespace a little to see.

Hope this helps.
 
W

Willy Denoyette [MVP]

Run your code first from a console application before you run this from
asp.net. What you need is a HRESULT code or a complete stack trace, the
"System.Runtime.InteropServices.COMException" is of little help really.

Willy.
 
I

iusfani

I have tried running the same code on console and it runs perfectly.
Now I think Nicholas was right. It was the ASPNET user's problem.

Tell me how to set the impersonation in web.config? Any idea?

Inam,
 
W

Willy Denoyette [MVP]

You don't need to impersonate, you simply have to use the DirectoryEntry
overload that takes a username and password to connect to the AD.

Willy.
 
I

iusfani

Thanks Willy,

And you are right, but you know I dont wanto keep my directory
information in there. What actually I have done is I have kept the
server and Authorization store's information in Web.Config file, like:

<appSettings>
<add key="Mode" value="AzMan" />
<add key="AzStore"
value="msldap://localhost:50000/CN=PHM,OU=CIMS,DC=testad,DC=com" />
...................
...................
</aapSettings>

Now, when user access ASP page, she has to provide user name to login.
Once login, it will determine the ADAM path from Web.Config, stores
information in a variable and then finally calls GetUserData(UserID)
method. (You've already seen this method in my first post).

Even after trying different ways, I could not get it done. Please tell
me your views about this.

I also tried to get it done by passing variable, that has stores
information, to the constructor as "DirectoryEntry(azStore)". But
nothing worked out.

Help me with that......Thanks in advance.

Inam,.
 

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

Top