2007-02-16 Geoff Norton <gnorton@customerdna.com>
[mono.git] / mcs / class / System / System.Net / BasicClient.cs
index 187d8fd35ac31b55ce9ae72164c9937a46b52424..5d89b269ff3fa143d72af4f78f415fd67d00cfae 100644 (file)
@@ -45,6 +45,16 @@ namespace System.Net
                        return InternalAuthenticate (webRequest, credentials);
                }
 
+               static byte [] GetBytes (string str)
+               {
+                       int i = str.Length;
+                       byte [] result = new byte [i];
+                       for (--i; i >= 0; i--)
+                               result [i] = (byte) str [i];
+
+                       return result;
+               }
+
                static Authorization InternalAuthenticate (WebRequest webRequest, ICredentials credentials)
                {
                        HttpWebRequest request = webRequest as HttpWebRequest;
@@ -52,6 +62,9 @@ namespace System.Net
                                return null;
 
                        NetworkCredential cred = credentials.GetCredential (request.AuthUri, "basic");
+                       if (cred == null)
+                               return null;
+
                        string userName = cred.UserName;
                        if (userName == null || userName == "")
                                return null;
@@ -62,9 +75,9 @@ namespace System.Net
 
                        // If domain is set, MS sends "domain\user:password". 
                        if (domain == null || domain == "" || domain.Trim () == "")
-                               bytes = Encoding.Default.GetBytes (userName + ":" + password);
+                               bytes = GetBytes (userName + ":" + password);
                        else
-                               bytes = Encoding.Default.GetBytes (domain + "\\" + userName + ":" + password);
+                               bytes = GetBytes (domain + "\\" + userName + ":" + password);
 
                        string auth = "Basic " + Convert.ToBase64String (bytes);
                        return new Authorization (auth);