我想为一个使用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); }但是,此示例不适用于Universal Apps(Win 10),我得到以下异常:
An error occurred while sending the request.当我看到InnerException时,我看到:
A security problem occurred. (Exception from HRESULT: 0x800C000E)有没有解决方案将我的应用程序连接到我的家庭服务器,如WinForms应用程序?
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?
最满意答案
请勿在URI中包含用户名和密码( RFC 3986 3.2.1用户信息 )。
相反,使用HttpClientHandler和NetworkCredential传递用户信息,即:
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);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);更多推荐
发布评论