This article is a mirror article of machine translation, please click here to jump to the original article.

View: 40852|Reply: 0

[ASP.NET] Ocelot Request headers must contain only ASCII characters

[Copy link]
Posted on 5/10/2021 3:24:29 PM | | | |
Review:

Ocelot Series (1) ASP.NET Core Gateway in practice
https://www.itsvse.com/thread-9284-1-1.html


When you start the ocelot gateway, the following error is reported for the access interface:
Status Code: 502; Bad Gateway                                                                                                                                                                                                                                                                                                                                                                                                                      
The project is ASP.NET Core 3.1,The version used by Ocelot is 16.0.1



info: Ocelot.RateLimit.Middleware.ClientRateLimitMiddleware[0]
      requestId: 0HM8JAOKL2C09:00000006, previousRequestId: no previous request id, message: EndpointRateLimiting is not enabled for /{url}
warn: Ocelot.Responder.Middleware.ResponderMiddleware[0]
      requestId: 0HM8JAOKL2C09:00000006, previousRequestId: no previous request id, message: Error Code: ConnectionToDownstreamServiceError Message: Error connecting to downstream service, exception: System.Net.Http.HttpRequestException: Request headers must contain only ASCII characters.
         at System.Net.Http.HttpConnection.WriteStringAsync(String s)
         at System.Net.Http.HttpConnection.WriteHeadersAsync(HttpHeaders headers, String cookiesFromContainer)
         at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
         at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
         at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
         at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
         at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
         at Ocelot.Requester.HttpClientHttpRequester.GetResponse(HttpContext httpContext) errors found in ResponderMiddleware. Setting error response for request path:/test1/home/index, request method: GET
Using Fiddler to catch packets as follows, I found that the cookie has a Chinese string, as follows:




Host: localhost:5002
Connection: keep-alive
Cache-Control: max-age=0
sec-ch-ua: "Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"
sec-ch-ua-mobile: ?0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Cookie: aaa=测试


The browser writes a cookie code (The following code value is not escaped) can reproduce the issue:

Browser console writes cookies
https://www.itsvse.com/thread-4682-1-1.html
Try to empty the cookie cache and the gateway returns to normal as shown in the image below:



The order of execution of the middleware with Ocelot is as follows:

PreErrorResponderMiddleware
PreAuthenticationMiddleware
AuthenticationMiddleware
PreAuthorisationMiddleware
AuthorisationMiddleware       
PreQueryStringBuilderMiddleware


(End)




Previous:ASP.NET Core gets the request full URL link
Next:Redis Benchmark performance test
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com