J
Jochen Hahnen
Hello,
I have a problem connecting to a webserver with basic authentication. I am
using the compactframework so I can not use the class CredentialCache (this
was working fine). First of all I was searching for a solution using
NetworkCredential:
Code :
try
{
Uri url = new Uri(@"http://some.url/server/somebscwserver.cgi");
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(@"http://some.url/bscw/bscw.cgi");
NetworkCredential myCredentials = new NetworkCredential( "username",
"password");
// the following 3 lines would do the job but are not supported
// CredentialCache myCredentialCache = new CredentialCache();
// MyCredentialCache.Add(url, "Basic", myCredential);
// req.Credentials = myCredentialCache;
// so I tried this:
req.Credentials = myCredentials;
System.IO.Stream response = req.GetResponse().GetResponseStream();
// throws the exeption
}
catch( Exception ex )
{
MessageBox.Show( ex.Message );
}
But now the application throws the following execption: "The remote server
returned an error: (401) Unauthorized." (What is wrong here ??? )
Ok so next I tried to build up the authorization Header myself:
Code:
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(@"http://some.url/bscw/bscw.cgi/")
req.Method = "GET";
string base64EncodedAuthorizationString = "username" + ":" + "password";
byte[] binaryData = new Byte[base64EncodedAuthorizationString.Length];
binaryData = Encoding.UTF8.GetBytes(base64EncodedAuthorizationString);
base64EncodedAuthorizationString = Convert.ToBase64String(binaryData);
base64EncodedAuthorizationString = "Basic " +
base64EncodedAuthorizationString;
req.Headers.Add("Authorization", base64EncodedAuthorizationString);
[...] some code for connection
The Header is now: "Authorization: Basic Sm9jaGVuOnppdmk=\r\n\r\n".
But I still get the same Error Message( 401 Unauthorized)
To be sure the header line is correct I tried the following header using
TelNet:
Header:
<-GET /bscw/bscw.cgi/ HTTP/1.1
Authorization: Basic Sm9jaGVuOnmk=
Host: someserver.url.de
where "Sm9jaGVuOnmk=" is the Base64Encode of "usernameassword"
And this works fine !!! That means I was able to authorize on the server.
Is the ".Headers"-class used by HttpWebRequest at all ? Because in any
Request using TelNet where I set up the authorization line above
(Authorization: Basic Sm9jaGVuOnmk=)
the authorization succeeded !
Thanks for your time and help,
Jochen Hahnen
I have a problem connecting to a webserver with basic authentication. I am
using the compactframework so I can not use the class CredentialCache (this
was working fine). First of all I was searching for a solution using
NetworkCredential:
Code :
try
{
Uri url = new Uri(@"http://some.url/server/somebscwserver.cgi");
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(@"http://some.url/bscw/bscw.cgi");
NetworkCredential myCredentials = new NetworkCredential( "username",
"password");
// the following 3 lines would do the job but are not supported
// CredentialCache myCredentialCache = new CredentialCache();
// MyCredentialCache.Add(url, "Basic", myCredential);
// req.Credentials = myCredentialCache;
// so I tried this:
req.Credentials = myCredentials;
System.IO.Stream response = req.GetResponse().GetResponseStream();
// throws the exeption
}
catch( Exception ex )
{
MessageBox.Show( ex.Message );
}
But now the application throws the following execption: "The remote server
returned an error: (401) Unauthorized." (What is wrong here ??? )
Ok so next I tried to build up the authorization Header myself:
Code:
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(@"http://some.url/bscw/bscw.cgi/")
req.Method = "GET";
string base64EncodedAuthorizationString = "username" + ":" + "password";
byte[] binaryData = new Byte[base64EncodedAuthorizationString.Length];
binaryData = Encoding.UTF8.GetBytes(base64EncodedAuthorizationString);
base64EncodedAuthorizationString = Convert.ToBase64String(binaryData);
base64EncodedAuthorizationString = "Basic " +
base64EncodedAuthorizationString;
req.Headers.Add("Authorization", base64EncodedAuthorizationString);
[...] some code for connection
The Header is now: "Authorization: Basic Sm9jaGVuOnppdmk=\r\n\r\n".
But I still get the same Error Message( 401 Unauthorized)
To be sure the header line is correct I tried the following header using
TelNet:
Header:
<-GET /bscw/bscw.cgi/ HTTP/1.1
Authorization: Basic Sm9jaGVuOnmk=
Host: someserver.url.de
where "Sm9jaGVuOnmk=" is the Base64Encode of "usernameassword"
And this works fine !!! That means I was able to authorize on the server.
Is the ".Headers"-class used by HttpWebRequest at all ? Because in any
Request using TelNet where I set up the authorization line above
(Authorization: Basic Sm9jaGVuOnmk=)
the authorization succeeded !
Thanks for your time and help,
Jochen Hahnen