Problem
I'm getting a strange 401 Unauthorized Error when using the Windows.Web.Http.HttpClient on Xbox One. On Windows Machines everything is working fine. Credentials are Ok and I've tested on 3 different Xbox One - every time the same result.
Here is the code:
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;
}
Here is the Fiddler Output (I have hardcoded username/pass zu avoid any typing errors).
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
Windows:
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
Thanks
I created a small app with a few lines of Code again. Now with System.NET.Http instead of Windows.Web.Http.
Strange Thing now is, that I get a result like this in fiddler:
Windows:
HTTP 401 Text/Html
HTTP 200 Text/x-json
Xbox:
HTTP 401 Text/Html
HTTP 401 Text/Html
Workaround:
I'm now using both methods, if one fails it's trying with the other:
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))));
But there seems to be a big problem on the Xbox One. I played around with the web server. If I set authentication to digest only on the server, the Xbox is unable to connect! So I think there is something wrong with generating the digest on the Xbox with Windows.Security.Credentials.PasswordCredential?
To make it clear:
Server Authentication Basic:
- Windows OK
- Xbox OK
Server Authentication Digest:
- Windows OK
- Xbox FAILED
Server Authentication Digest + Basic:
- Windows OK (Authenticated with Digest)
- Xbox OK (Authenticated with Basic Auth)