Skip to content

"WinHttpException: The parameter is incorrect" when server return 401 #18362

@TingluoHuang

Description

@TingluoHuang

I have a customized HttpMessageHandler that handle 401 from http response and throw a particular exception to provide better error message for end user. The code works fine with full desktop .net 4.5, however after ported to dotnet core i am getting the "WinHttpException: The parameter is incorrect" error when server return 401.
Here is the full call stack when 401 happen for both dotnet core app and .net 4.5 app:

dotnet core:
[2016-08-30 17:28:45Z ERR  Terminal] System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.WinHttpException: The parameter is incorrect
   at System.Net.Http.WinHttpAuthHelper.SetWinHttpCredential(SafeWinHttpHandle requestHandle, ICredentials credentials, Uri uri, UInt32 authScheme, UInt32 authTarget)
   at System.Net.Http.WinHttpAuthHelper.CheckResponseForAuthentication(WinHttpRequestState state, UInt32& proxyAuthScheme, UInt32& serverAuthScheme)
   at System.Net.Http.WinHttpHandler.<StartRequest>d__101.MoveNext()
   --- End of inner exception stack trace ---
   at Microsoft.VisualStudio.Services.Common.VssHttpRetryMessageHandler.<SendAsync>d__3.MoveNext() in C:\VSO\src\Vssf\Client\Common\VssHttpRetryMessageHandler.cs:line 150
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.Http.HttpClient.<FinishSendAsync>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<SendAsync>d__45.MoveNext() in C:\VSO\src\Vssf\Client\WebApi\VssHttpClientBase.cs:line 762
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<SendAsync>d__42`1.MoveNext() in C:\VSO\src\Vssf\Client\WebApi\VssHttpClientBase.cs:line 663
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<SendAsync>d__27`1.MoveNext() in C:\VSO\src\Vssf\Client\WebApi\VssHttpClientBase.cs:line 383
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<SendAsync>d__26`1.MoveNext() in C:\VSO\src\Vssf\Client\WebApi\VssHttpClientBase.cs:line 366
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.VisualStudio.Services.Agent.Listener.Configuration.ConfigurationManager.<GetPoolId>d__11.MoveNext() in C:\vsts-agent\src\Agent.Listener\Configuration\ConfigurationManager.cs:line 461
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.VisualStudio.Services.Agent.Listener.Configuration.ConfigurationManager.<ConfigureAsync>d__7.MoveNext() in C:\vsts-agent\src\Agent.Listener\Configuration\ConfigurationManager.cs:line 152

.net 4.5:
Microsoft.VisualStudio.Services.Common.VssUnauthorizedException: VS30063: You are not authorized to access https://ting.tfsbuildhost8.tfsallin.net.
   at Microsoft.VisualStudio.Services.Common.VssHttpMessageHandler.<SendAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.Services.Common.VssHttpRetryMessageHandler.<SendAsync>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<SendAsync>d__45.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<SendAsync>d__42`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<SendAsync>d__27`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.<SendAsync>d__26`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.Services.WebApi.TaskExtensions.SyncResult[T](Task`1 task)
   at ConsoleApplication9.Program.Main(String[] args) in C:\Users\tihuang\Documents\Visual Studio 2015\Projects\ConsoleApplication9\ConsoleApplication9\Program.cs:line 105

The Fiddler trace for both cases:

dotnet core:
GET https://ting.tfsbuildhost8.tfsallin.net/_apis/distributedtask/pools?poolName=default HTTP/1.1
Connection: Keep-Alive
Accept: application/json; api-version=3.0-preview.1
Accept-Encoding: gzip, peerdist
Accept-Language: en-US
Authorization: Basic VnN0c0FnZW50OmxscGQzb2djY2twdnJyNHh6NGtmeXpjeGdjN252cTNpYWVucHpqeXk0ZXk3emlqeXVmcmE=
User-Agent: VSServices/15.255.65000.0 (NetStandard) VstsAgentCore-win7-x64/2.106.0 (Microsoft Windows 10.0.14393)
X-TFS-FedAuthRedirect: Suppress
X-TFS-Session: 43b8fed2-d9d7-4582-9c91-6538cdd26e79
X-P2P-PeerDist: Version=1.1
X-P2P-PeerDistEx: MinContentInformation=1.0, MaxContentInformation=2.0
Host: ting.tfsbuildhost8.tfsallin.net

HTTP/1.1 401 Unauthorized
Server: Microsoft-IIS/8.5
X-TFS-ProcessId: a3f879d3-c2d2-4c93-a872-671dd4b52407
Strict-Transport-Security: max-age=31536000; includeSubDomains
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 3600
Access-Control-Allow-Methods: OPTIONS,GET,POST,PATCH,PUT,DELETE
Access-Control-Expose-Headers: ActivityId,X-TFS-Session,X-MS-ContinuationToken
Access-Control-Allow-Headers: authorization
X-FRAME-OPTIONS: SAMEORIGIN
WWW-Authenticate: Basic realm="https://app.tfsbuildhost8.tfsallin.net/"
X-Powered-By: ASP.NET
P3P: CP="CAO DSP COR ADMa DEV CONo TELo CUR PSA PSD TAI IVDo OUR SAMi BUS DEM NAV STA UNI COM INT PHY ONL FIN PUR LOC CNT"
X-Content-Type-Options: nosniff
Date: Tue, 30 Aug 2016 17:17:37 GMT
Content-Length: 0

.net 4.5:
GET https://ting.tfsbuildhost8.tfsallin.net/_apis/distributedtask/pools HTTP/1.1
Accept: application/json; api-version=3.0-preview.1
User-Agent: VSServices/15.104.25627.0 (ConsoleApplication9.exe)
Accept-Language: en-US
X-TFS-FedAuthRedirect: Suppress
X-TFS-Session: 7b7a5d6a-e30f-4511-bf16-6396c91317b2
X-VSS-Agent: VSS: fe82a6ab-72ed-4ce2-8e28-d626d07f5d24
Authorization: Basic VnN0c0FnZW50OmxscGQzb2djY2twdnJyNHh6NGtmeXpjeGdjN252cTNpYWVucHpqeXk0ZXk3emlqeXVmcmE=
Host: ting.tfsbuildhost8.tfsallin.net
Accept-Encoding: gzip

HTTP/1.1 401 Unauthorized
Server: Microsoft-IIS/8.5
X-TFS-ProcessId: a3f879d3-c2d2-4c93-a872-671dd4b52407
Strict-Transport-Security: max-age=31536000; includeSubDomains
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 3600
Access-Control-Allow-Methods: OPTIONS,GET,POST,PATCH,PUT,DELETE
Access-Control-Expose-Headers: ActivityId,X-TFS-Session,X-MS-ContinuationToken
Access-Control-Allow-Headers: authorization
X-FRAME-OPTIONS: SAMEORIGIN
WWW-Authenticate: Basic realm="https://app.tfsbuildhost8.tfsallin.net/"
X-Powered-By: ASP.NET
P3P: CP="CAO DSP COR ADMa DEV CONo TELo CUR PSA PSD TAI IVDo OUR SAMi BUS DEM NAV STA UNI COM INT PHY ONL FIN PUR LOC CNT"
X-Content-Type-Options: nosniff
Date: Tue, 30 Aug 2016 17:17:11 GMT
Content-Length: 0

Also, the "WinHttpException: The parameter is incorrect" doesn't tell me which parameter is incorrect which cause the exception message a little bit useless.
I can provide repro environment if needed.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions