http 请求和 Universal Apps 出现安全错误

Security error occured with http request and Universal Apps

我想为使用 Domoticz 的 domotic 项目创建自己的远程应用程序,并使用 C# 中的通用应用程序 (Win 10)。 我使用 Basic-Auth,它与 WinForm 或 WPF 项目完美配合,我可以连接并获取(在这种情况下)或在服务器中设置值:

    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);
    }

但是,此示例不适用于通用应用程序 (Win 10),我收到此异常:

An error occurred while sending the request.

当我查看 InnerException 时,我看到了:

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

是否有像 WinForms 应用程序一样将我的应用程序连接到我的家庭自动化服务器的解决方案?

不要在 URI 中包含用户名和密码 (RFC 3986 3.2.1 User Information)。

相反,使用 HttpClientHandlerNetworkCredential 来传递用户信息,即:

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);

从 URI 中删除 @ -

see string uri = @"http://username:password192.168.1.1:8080/json.htm?type=devices&filter=all&used=true&order=Name";