【发布时间】:2017-03-13 19:05:24
【问题描述】:
我有一个 ASP.NET Core MVC 应用程序,它使用集成 Windows 身份验证并调用托管在同一 IIS 服务器上的 Web API(因此对 API 调用使用 WindowsIdentity Impersonation,这也需要身份验证)。大多数路由都有效,但如果执行更新或创建操作并且我尝试将用户重定向到新创建的项目,则会收到 502 Bad Gateway 错误。 POST/PUT 命令通过 Web API 并响应 MVC 应用程序,所以我认为这是 IIS 配置问题,或者路由有问题。
[HttpPost]
public async Task<IActionResult> CreateIncident(Incident model)
{
HttpResponseMessage response = null;
var identity = User.Identity as WindowsIdentity;
async Task Action()
{
response = await _service.CreateIncident(model);
}
async Task GetId()
{
model.IncidentTrackingRefId = await _service.GetNewIncidentId(model.IncidentCategoryLookupTableId,
model.IncidentTypeLookupTableId);
}
await WindowsIdentity.RunImpersonated(identity.AccessToken, GetId);
await WindowsIdentity.RunImpersonated(identity.AccessToken, Action);
if (response == null) return RedirectToAction("Error", "Home");
if (response.StatusCode == HttpStatusCode.Created)
{
return RedirectToAction("View", "Incidents", new { id = model.IncidentId });
}
}
查看操作:
[HttpGet]
public async Task<IActionResult> View(int id)
{
var identity = User.Identity as WindowsIdentity;
async Task Action()
{
ViewBag.BusTypes = await _service.GenerateDropDown("/GetIncidentBusTypes");
}
Incident incident = null;
async Task GetIncident()
{
incident = await _service.GetIncidentById(id);
}
await WindowsIdentity.RunImpersonated(identity.AccessToken, GetIncident);
await WindowsIdentity.RunImpersonated(identity.AccessToken, Action);
if (ViewBag.BusTypes == null || incident == null) return RedirectToAction("Error", "Home");
return View(incident);
}
【问题讨论】:
-
RedirectToAction导致将 302 响应发送到客户端,并将指向操作集的 URL 路由设置为Location标头。这是标准的 HTTP 内容。至此,请求-响应周期完成。但是,通常情况下,客户端随后会针对Location标头中的 URL 发出新的 GET 请求。看起来您正在尝试重定向到仅使用有效负载响应 POST 的操作。两者都不可能。 -
我在我的 OP 中添加了 View() 操作。我在我的代码中将它指定为带有属性标记的 HttpGet,所以我认为您关于重定向的观点在这种情况下是不正确的。
-
@RobertMcCoy 您提到的 MVC 应用程序和 WebAPI 应用程序是两个独立的 IIS Web 应用程序?
-
@laika 是的,使用集成 Windows 身份验证和针对 AD 组进行验证以进行授权。 WindowsIdentity.RunImpersonated() 将身份验证发送到 Web API。
标签: asp.net asp.net-mvc asp.net-core