【发布时间】:2018-05-18 15:13:43
【问题描述】:
问题
在 Xbox One 上使用 Windows.Web.Http.HttpClient 时,我遇到了一个奇怪的 401 Unauthorized Error。在 Windows 机器上一切正常。凭证没问题,我已经在 3 种不同的 Xbox One 上进行了测试 - 每次都是相同的结果。
代码如下:
Windows.Web.Http.Filters.HttpBaseProtocolFilter filter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
filter.AllowUI = false;
filter.CacheControl.WriteBehavior = Windows.Web.Http.Filters.HttpCacheWriteBehavior.NoCache;
filter.CacheControl.ReadBehavior = Windows.Web.Http.Filters.HttpCacheReadBehavior.NoCache;
Uri uri = new Uri("http://" + url + ":" + port + endpoint);
if (username != "" && password != "")
filter.ServerCredential = new Windows.Security.Credentials.PasswordCredential(uri.OriginalString, username, password);
try
{
HttpClient client = new HttpClient(filter);
HttpResponseMessage response = await client.GetAsync(uri);
response.EnsureSuccessStatusCode();
var buffer = await response.Content.ReadAsBufferAsync();
var byteArray = buffer.ToArray();
return Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);
}
catch
{
return null;
}
这是 Fiddler 输出(我有 硬编码 用户名/密码以避免任何输入错误)。
Xbox One:
GET http://192.168.178.31:XXXX/XXX/XXX HTTP/1.1
Accept-Encoding: gzip, deflate
Host: 192.168.178.31:XXXX
Connection: Keep-Alive
Pragma: no-cache
HTTP/1.1 401 Unauthorized
Server: XXXX
Cache-Control: no-cache
WWW-Authenticate: Digest realm="XXXX", qop="auth", nonce="516f32c0f120024c220873c1ebc159e4", opaque="2effb29f8b3de0ca6a688330875890c8"
Connection: Keep-Alive
Content-Type: text/html
Content-Length: 432
窗户:
GET http://192.168.178.31:XXXX/XXX/XXX HTTP/1.1
Accept-Encoding: gzip, deflate
Host: 192.168.178.31:XXXX
Connection: Keep-Alive
Pragma: no-cache
HTTP/1.1 200 OK
谢谢
我再次用几行代码创建了一个小应用程序。现在使用 System.NET.Http 而不是 Windows.Web.Http。
现在奇怪的是,我在 fiddler 中得到了这样的结果:
窗户:
HTTP 401 Text/Html
HTTP 200 Text/x-json
Xbox:
HTTP 401 Text/Html
HTTP 401 Text/Html
解决方法:
我现在正在使用这两种方法,如果一种方法失败了,它会尝试另一种方法:
if (!digestFailed)
filter.ServerCredential = new Windows.Security.Credentials.PasswordCredential(uri.OriginalString, username, password);
else
request.Headers.Authorization = new HttpCredentialsHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", username, password))));
但 Xbox One 上似乎有一个大问题。我玩弄了网络服务器。如果我在服务器上将身份验证设置为 digest only,则 Xbox 无法 连接!所以我认为使用 Windows.Security.Credentials.PasswordCredential 在 Xbox 上生成摘要有问题吗?
说清楚:
-
服务器认证基础:
- Windows 确定
- Xbox 好的
-
服务器身份验证摘要:
- Windows 确定
- Xbox 失败
-
服务器认证摘要+基本:
- Windows 确定(已通过摘要验证)
- Xbox OK(通过基本身份验证)
【问题讨论】:
-
而不是
catching 所有异常而不记录任何内容。通过调试/断点或将异常的内容记录到控制台,至少尝试catch (Exception e)并检查e元素。 您可以在以下位置找到有关异常及其属性的信息: C# Exception Properties