Udostępnij za pośrednictwem


401 Unauthorized Error using WebRequest when accessing local SharePoint site

 

Colleague of mine asked me to research this issue and thought of sharing the solution as this is not clearly discussed anywhere on the Internet.

Problem:

Accessing local SharePoint site (with Windows Integrated Authentication) works fine using IE, however if the same site is accessed via .NET console application using HttpWebRequest object it throws 401 Unauthorized error.

Solution:

I tried bunch of things such as DefaultCredentials, NetWorkCredentials and ImpersonationTokenType etc but none would work. Apparently it was not a code but it was something very simple. Guess what! it has to do with custom host entry. Weird huh?

Here’s the KB article that resolved the issue (followed method 2 and restart the machine)

https://support.microsoft.com/default.aspx?scid=kb;en-us;896861

Details:

Usually SharePoint developers uses single VPC and create web application with URL entry like https://Litware and adds an entry of Litware in Host file to point back to 127.0.0.1. This works great for usual SharePoint development.

The IE works great as it automatically uses integrated authentication. However, for console application it doesn’t work when the Web site uses Integrated Authentication and has a name that is mapped to the local loopback address.

[Remark: This solution is only been tested on Windows 2003 R2, MOSS 2007 SP1 and December Cumulative Update, .Net 3.5 Sp1.]

Technorati Tags: SharePoint WebRequest 401 Unauthorized