【问题标题】:Security error occured with http request and Universal Appshttp 请求和通用应用程序发生安全错误
【发布时间】:2015-12-18 23:39:47
【问题描述】:

我想为一个使用 Domoticz 的 domotic 项目创建自己的远程应用程序,并在 C# 中使用 Universal Apps (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 应用程序?

【问题讨论】:

    标签: c# http-headers httprequest win-universal-app


    【解决方案1】:

    不要在 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);
    

    【讨论】:

      【解决方案2】:

      从 URI 中删除 @ -

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-23
        • 1970-01-01
        • 2020-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多