RE: "401 unauthorized access" error

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Bruce Johnson [C# MVP] (bruce_at_spammenot.objectsharp.com)
Date: 02/03/05


Date: Thu, 3 Feb 2005 11:39:04 -0800

I don't see where you're specifying any credentials associated with your HTTP
request. Try setting the HttpWebRequest.Credentials =
System.Net.CredentialCache.DefaultCredentials before making the call.

Hope this helps.

"ag" wrote:

> Hi guys.. need your help.
>
> We have abc.asp page on the source server which calls a .net component on that server. The component is in MTS running under say "domain2\id1" identity. The component uses HttpWebRequest to call xyz.asp on the target server. Both servers are under the same domain say domain1 and there is a trust between domain1 and domain2.
>
> The problem is the target server is returning 401 unauthorized access when calling xyz.asp from the component which is on the source server. It is running fine when anonymous authentication is set on the target server vdir but throwing the above error with integrated win auth which is what we want it to run under. The source server vdir is also set to integrated win auth.
>
> Here is the C# component code for your ref:
> ------------------------------------------------
> //Create the Web request object. Use the Create method instead of the constructor
> HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(strURL);
>
> //Set the keep alive property to false to prevent caching of credentials
> myHttpWebRequest.KeepAlive = false;
>
> //Set the method to POST
> myHttpWebRequest.Method = "POST";
>
> //Set the proxy settings
> string[] arrByPassLists = {cstrProxyByPassList};
> WebProxy myWebProxy = new WebProxy(cstrProxySrvrUri, true, arrByPassLists);
> myHttpWebRequest.Proxy = myWebProxy;
>
> //Set the timeout value (in milliseconds)
> myHttpWebRequest.Timeout = cintTimeOutVal;
>
> //Set the content type
> myHttpWebRequest.ContentType = "application/x-zip-compressed"; //TODO: Set the content type to binary
>
> //Write all the zip file data to the request string
>
> //Open the zip file for reading
> FileStream myFileStream = File.OpenRead(strZipPath);
>
> //Open the request stream for writing
> Stream myRequestStream = myHttpWebRequest.GetRequestStream();
>
> //Loop through to write the zip file data
> byte[] myBuffer = new byte[cintBufferLen];
> int intBytesRead;
> long lngTotalSize = 0;
> intBytesRead = myFileStream.Read(myBuffer, 0, cintBufferLen);
> while(intBytesRead > 0)
> {
> lngTotalSize+= intBytesRead;
> myRequestStream.Write(myBuffer, 0, intBytesRead);
> intBytesRead = myFileStream.Read(myBuffer, 0, cintBufferLen);
> };
>
> //Close the streams
> myFileStream.Close();
> myRequestStream.Close();
>
> //End of writing the zip file data to the stream
>
> //Contact the server and get the response from it
> WebResponse myWebResponse;
> string strResponse;
>
> //Get the response
> myWebResponse = myHttpWebRequest.GetResponse() //Getting 401 error here in response
> ---------------------------------------
>
> Let me know if you need any more info...
>
> Thanks much.
> AG.
>
> Posted via DevelopmentNow Groups
> www.developmentnow.com/g
> www.developmentnow.com
>