Tim
Tim

Reputation: 163

Why do I get a 401 Unauthorised error calling the Bing Geocode service from SharePoint but not from a WinForms app?

I'm developing a SharePoint Timer Job to geocode some lists of addresses held on our SharePoint site.

I'm using code based on this MSDN sample to do the actual Geocoding request.

The problem I get is that when I call the service from SharePoint 2010 (running locally) I get a 401 unauthorised error in return.

Interestingly, I have also created a small winforms application which does the same thing (but without SharePoint/IIS) using the same code which works perfectly.

I'm setting the credentials the same way on both apps as follows:

request.Proxy.Credentials = CredentialCache.DefaultCredentials;

I'm an enterprise user and I'm using the same key on both apps - but one works and one doesn't - any ideas why this might be? Is it something i need to set in IIS perhaps?

I tried setting Pipelined = false on the request which was a suggestion I read about on but that didn't seem to work.

Any suggestions gratefully accepted.

Upvotes: 2

Views: 1042

Answers (1)

Shiraz Bhaiji
Shiraz Bhaiji

Reputation: 65361

Looks like you solved it by changing the identity of the application pool.

The other way to do it is to say that the web service call should be made using the identity of the user that sent the request to SharePoint. To do that try something like:

 using (((WindowsIdentity)HttpContext.Current.User.Identity).Impersonate())
 {
    WCFTestService.ServiceClient myService = new WCFTestService.ServiceClient();
    Response.Write(myService.GetData(123) + "<br/>");
    myService.Close();
 }

Upvotes: 1

Related Questions