Security error occured with http request and Universal Apps

0

I want to create my own remote app for a domotic project which uses Domoticz, with Universal Apps (Win 10) in C#. I use Basic-Auth and it works perfectly with WinForm or WPF project, I can connect and get (in this case) or set values in the server :

    private async void request()
    {
        string uri = @"http://username:password@192.168.1.1:8080/json.htm?type=devices&filter=all&used=true&order=Name";
        HttpClient client = new HttpClient();
        string token = Convert.ToBase64String(Encoding.ASCII.GetBytes("username:password"));
        client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", token);

        string body = await client.GetStringAsync(uri);
    }

However, this sample doesn't work with Universal Apps (Win 10), I get this exception :

An error occurred while sending the request.

When I look at InnerException I see that :

A security problem occurred. (Exception from HRESULT: 0x800C000E)

Is there a solution to connect my app to my domotic server like WinForms apps?

c#
http-headers
httprequest
win-universal-app
asked on Stack Overflow Dec 18, 2015 by Thomas Capodano

2 Answers

0

Do not include the user name and password in the URI (RFC 3986 3.2.1 User Information).

Instead, use a HttpClientHandler and a NetworkCredential to pass the user info, i.e.:

Uri uri = new Uri(
    "http://192.168.1.1:8080/json.htm?type=devices&filter=all&used=true&order=Name");

HttpClientHandler handler = new HttpClientHandler();

handler.Credentials = new System.Net.NetworkCredential(
    "username",
    "password");

HttpClient client = new HttpClient(handler);

await httpClient.GetAsync(uri);
answered on Stack Overflow Dec 19, 2015 by kiewic
0

Remove @ from the URI -

see string uri = @"http://username:password192.168.1.1:8080/json.htm?type=devices&filter=all&used=true&order=Name";
answered on Stack Overflow Jun 20, 2016 by Sprint • edited Jun 20, 2016 by Pirate X

User contributions licensed under CC BY-SA 3.0