Challouatte Cyril
Challouatte Cyril

Reputation: 127

Basic authentication with HttpWebRequest

I'm making a postman like application and i am trying to implement the basic authentication using HttpWebRequest but i always end with a 403 forbidden error which, i guess, means that i do something wrong.

I am currently using WebHeaderCollection to store my headers. I build my Authorization header like this :

code :

private WebHeaderCollection AddAuthorization(WebHeaderCollection wc)
    {
        if (expanderBasique.IsExpanded && !String.IsNullOrEmpty(BasiqueUserName.Text) && !String.IsNullOrEmpty(BasiquePassword.Password))
            {
                String username = BasiqueUserName.Text.Trim();
                String password = BasiquePassword.Password.Trim();
                String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
                wc.Add("Authorization", "Basic " + encoded);
            }

            return wc;
        }

Then I link my collection to the HttpWebRequest :

code :

private async Task<WebResponse> ExecuteHttpWebRequest()
        {
            var selectedMethode = Methode.SelectedItem as TextBlock;
            string method = selectedMethode.Text;
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Query.Text);
            WebHeaderCollection myWebHeaderCollection = webRequest.Headers;
            myWebHeaderCollection = BuildHeaderCollection(myWebHeaderCollection);
            myWebHeaderCollection = AddAuthorization(myWebHeaderCollection);
            webRequest.PreAuthenticate = true;
            webRequest.Method = method;
            if (webRequest.Method == "POST" || webRequest.Method == "PUT" || webRequest.Method == "PATCH")
            {
                byte[] data = Encoding.ASCII.GetBytes(Body.Text);
                webRequest.ContentLength = data.Length;
                using (var stream = webRequest.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }
            }
            return await webRequest.GetResponseAsync();
        }

I try this code with the GitHub api with my own credentials : https://api.github.com/user

With other apps like postman or restER I get my wanted results but i always end up with the 403 error in my own.

I really want to stick with HttpWebRequest.
I tried various solutions from Stack Overflow without success : Here or here

Upvotes: 1

Views: 665

Answers (1)

Challouatte Cyril
Challouatte Cyril

Reputation: 127

Edit :
Based on the FaizanRabbani comment, i resolved the problem by setting an UserAgent property to the httpwebrequest :

code :

private async Task<WebResponse> ExecuteHttpWebRequest()
        {
            var selectedMethode = Methode.SelectedItem as TextBlock;
            string method = selectedMethode.Text;
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Query.Text);
            WebHeaderCollection myWebHeaderCollection = webRequest.Headers;
            myWebHeaderCollection = BuildHeaderCollection(myWebHeaderCollection);
            myWebHeaderCollection = AddAuthorization(myWebHeaderCollection);
            webRequest.PreAuthenticate = true;
            webRequest.Method = method;
            webRequest.UserAgent = "something";
            if (webRequest.Method == "POST" || webRequest.Method == "PUT" || webRequest.Method == "PATCH")
            {
                byte[] data = Encoding.ASCII.GetBytes(Body.Text);
                webRequest.ContentLength = data.Length;
                using (var stream = webRequest.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }
            }
            return await webRequest.GetResponseAsync();
        }

Upvotes: 2

Related Questions