Click here to Skip to main content
15,888,251 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

We are getting 401 (unauthorised access) error while trying to access edms or SharePoint link from code behind, following is the block of code that is used to access the edms or SharePoint link.

C#
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(txtLink.Text);
request.UseDefaultCredentials = true;
request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
request.ImpersonationLevel = TokenImpersonationLevel.Delegation;
CookieContainer cookiescont = new CookieContainer();
request.CookieContainer = cookiescont;
request.AllowWriteStreamBuffering = false;
request.PreAuthenticate = true;
request.AllowAutoRedirect = true;
WebResponse wsc = request.GetResponse();


Note: Following are the environmental and other details.
1. Website is devloped in .net 3.5
2. The website is hosted on IIS 6.0 AND Windows 2003.
3. Following is the setting in web.config
XML
<authentication mode="Windows">
</authentication>
<authorization>
    <deny users="?" />
</authorization>


4. Surprisingly: Same block of code is working on my local machine which have XP and IIS 5.1

Any help will be greatly appreciated.

Regards,
Anuj Chauhan
[contact details removed]
Posted
Updated 19-May-10 21:35pm
v2

1 solution

Sounds like the credentials you are using for your request are not correct. When running on the web server rather than your development machine, System.Net.CredentialCache.DefaultNetworkCredentials is going to pick up the credentials from the server rather than the user executing the request.

Perhaps add some debugging to confirm exactly what account is being assigned to request.Credentials when run on the web server.

Have a look at this post on stack overflow

http://stackoverflow.com/questions/1058636/using-defaultcredentials-and-defaultnetworkcredentials[^]

That links to an MSDN article explaining impersonation \ delegation

http://msdn.microsoft.com/en-us/library/ms998351.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900