HttpWebRequest using Basic authentication

You can also just add the authorization header yourself.

Just make the name “Authorization” and the value “Basic BASE64({USERNAME:PASSWORD})”

var username   = "abc";
var password   = "123";
string encoded = System.Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1")
                               .GetBytes(username + ":" + password));
httpWebRequest.Headers.Add("Authorization", "Basic " + encoded);

Edit

Switched the encoding from UTF-8 to ISO 8859-1 per What encoding should I use for HTTP Basic Authentication? and Jeroen’s comment.

Leave a Comment