[Mono-devel-list] RFC 2617 basic auth in System.Net.HttpWebRequest

Mohammad DAMT md at mt.web.id
Mon May 19 06:35:43 EDT 2003


Basic auth in System.Net.HttpWebRequest can be set from Credentials 
property.
Currently this property does nothing and sends no auth info (RFC2617) 
to the server which will cause http error 401.
Patch to accomodate such auth method is attached. I have no idea is it 
the right place to set the http header in the set block of Credentials 
property.

Mohammad DAMT <mdamt at bisnisweb.com>
-------------- next part --------------
--- _HttpWebRequest.cs	2003-05-19 17:26:19.000000000 +0700
+++ HttpWebRequest.cs	2003-05-19 17:23:04.000000000 +0700
@@ -167,7 +167,24 @@ namespace System.Net 
 		
 		public override ICredentials Credentials { 
 			get { return credentials; }
-			set { credentials = value; }
+			set { credentials = value; 
+				if (credentials is NetworkCredential && credentials != null) {
+		 			if (((NetworkCredential) credentials).UserName != null) {
+						if (webHeaders ["Authorization"] != null && webHeaders ["Authorization"].Length > 0)
+							webHeaders.RemoveInternal ("Authorization");
+
+	 					string user_pass_string = ((NetworkCredential) credentials).UserName + ":";
+						if (((NetworkCredential) credentials).Password != null) 
+	 						user_pass_string += ((NetworkCredential) credentials).Password;
+
+						byte[] user_pass = new byte[user_pass_string.Length];
+						for (int i = 0; i < user_pass_string.Length; i++) 
+	 						user_pass [i] = (byte) user_pass_string [i];
+							
+						webHeaders.SetInternal ("Authorization", "Basic " +  + Convert.ToBase64String (user_pass));
+					}
+				}
+			}
 		}
 		
 		public string Expect {


More information about the Mono-devel-list mailing list