2

I'm trying to do a GET in an UWP (Windows 10) app. I've tried several ways but all always return 401.

In Postman it works fine, but I can' seem to get it to work in my app. What am I missing.

These are the methods I tried (all return 401):

Method 1:

        var request = WebRequest.Create("http://api.fos.be/person/login.json?login=200100593&password=pass");
        request.Headers["Authorization"] = "Basic MYAUTHTOKEN";
        var response = await request.GetResponseAsync();

Method 2:

        const string uri = "http://api.fos.be/person/login.json?login=200100593&password=pass";
        var httpClientHandler = new HttpClientHandler();
        httpClientHandler.Credentials = new System.Net.NetworkCredential("MYUSERNAME", "MYPASSWORD");
        using (var client = new HttpClient(httpClientHandler))
        {
            var result = await client.GetAsync(uri);
            Debug.WriteLine(result.Content);

        }

Method 3:

        var client = new RestClient("http://api.fos.be/person/login.json?login=200100593&password=pass");
        var request = new RestRequest(Method.GET);
        request.AddHeader("postman-token", "e2f84b21-05ed-2700-799e-295f5470c918");
        request.AddHeader("cache-control", "no-cache");
        request.AddHeader("authorization", "Basic MYAUTHTOKEN");
        IRestResponse response = await client.Execute(request);
        Debug.WriteLine(response.Content);

The third method is code generated straight from Postman, so why is it working there and not in my app?

1
  • As @Aaron provided may be the right solution and please also confirm the username and password are right.
    – Sunteen Wu
    Nov 17, 2016 at 7:16

2 Answers 2

1

This thread helped me figure out the solution. I was using http:// but I had to make it https://. HTTPS with the code in that thread was the solution.

This is my final code:

    public static async void GetPerson()
    {
        //System.Diagnostics.Debug.WriteLine("NetworkConnectivityLevel.InternetAccess: " + NetworkConnectivityLevel.InternetAccess);
        //use this, for checking the network connectivity
        System.Diagnostics.Debug.WriteLine("GetIsNetworkAvailable: " + System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable());
        //var msg = new Windows.UI.Popups.MessageDialog("GetIsNetworkAvailable: " + System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable());
        //msg.ShowAsync();
        HttpClient httpClient = new HttpClient();
        // Assign the authentication headers
        httpClient.DefaultRequestHeaders.Authorization = CreateBasicHeader("MYUSERNAME", "MYPASS");
        System.Diagnostics.Debug.WriteLine("httpClient.DefaultRequestHeaders.Authorization: " + httpClient.DefaultRequestHeaders.Authorization);
        // Call out to the site
        HttpResponseMessage response = await httpClient.GetAsync("https://api.fos.be/person/login.json?login=usern&password=pass");
        System.Diagnostics.Debug.WriteLine("response: " + response);
        string responseAsString = await response.Content.ReadAsStringAsync();
        System.Diagnostics.Debug.WriteLine("response string:" + responseAsString);
    }
    public static AuthenticationHeaderValue CreateBasicHeader(string username, string password)
    {
        byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(username + ":" + password);
        String logindata = (username + ":" + password);
        System.Diagnostics.Debug.WriteLine("AuthenticationHeaderValue: " + new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)));
        return new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
    }
1

I would try this first:

Check your "MYAUTHTOKEN", it is usually a combo of username:password and is base 64 encoded. So if your username was "user" and password was "pass" you would need to base64 encode "user:pass"

var request = WebRequest.Create("https://api.fos.be/person/login.json");
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Text.Encoding.UTF8.GetBytes("user:pass"));
var response = await request.GetResponseAsync();
5
  • This doesn't work, it gives the following error: "A security problem occurred. (Exception from HRESULT: 0x800C000E)"
    – vixez
    Nov 17, 2016 at 9:46
  • @Aaron Shouldn't ?login=200100593&password=pass be removed when using Basic Auth? Nov 17, 2016 at 15:13
  • @ThomasSchneiter, yes i would think that is unnecessary. I will edit the post accordingly.
    – Aaron
    Nov 17, 2016 at 17:56
  • @vixez ah, apparently the WebRequest class will not support the credentials in the uri. I have updated the answer.
    – Aaron
    Nov 17, 2016 at 18:01
  • Okay thanks, but the parameters are required for the server. I have found the answer meanwhile. I've posted the answer but cant accept it as answer until tomorrow. Thanks anyway.
    – vixez
    Nov 17, 2016 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.