502 Bad Gateway on POST/PUT Web API Calls from ASP

2019-05-30 18:56发布

I'm getting strange errors in ASP.NET Core when calling Web API that I have created for the application. GET requests go through fine and return all of the data that they should, but my POST/PUT commands all return a 502, specifically from the MVC application. I can call the API's from Postman and get a proper response and the object is created in the database.

502 - Web server received an invalid response while acting as a gateway or proxy server. There is a problem with the page you are looking for, and it cannot be displayed. When the Web server (while acting as a gateway or proxy) contacted the upstream content server, it received an invalid response from the content server.

I am impersonating an Integrated Windows Login with the following code for all web requests to the API:

async Task Action()
{
    response = await _service.CreateIncident(model);
}

await WindowsIdentity.RunImpersonated(identity.AccessToken, Action);

CreateIncident(model):

using (var client = new HttpClient(new HttpClientHandler { UseDefaultCredentials = true }))
        {
            var newIncident = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json");
            var response = await client.PostAsync(hostUri, newIncident);
            return response;
        }

There is also one GET Request that I make through Ajax to get an incremented ID to display to the user before they create their new Incident that returns a 502 Bad Gateway as well. Is this an IIS Setting that is incorrect?

2条回答
疯言疯语
2楼-- · 2019-05-30 19:12

If you use WindowsIdentity.RunImpersonated and an asynchronous function, it will not work. You must be synchronous when doing non-GET requests. I have updated my GitHub issue, I'm hoping to get this bug addressed. If you are a future visitor to this topic, you can see where this ended up here.

查看更多
迷人小祖宗
3楼-- · 2019-05-30 19:31

I think it also depends on the size of the data. Smaller packages work, larger ones don't.

查看更多
登录 后发表回答