我一直在尝试防止在控制器返回NotAuthorized IActionResult时重定向,但是无论我如何尝试,NotAuthorized 都会被转换为重定向。
我尝试了这里提到的方法(相同问题,使用旧版beta框架,我使用的是1.0.0-rc1-final)。我没有Notifications名称空间(已在rc1-final中删除)。
这是我的登录控制器:
[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
if (ModelState.IsValid)
{
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
return Ok(model);
}
if (result.IsLockedOut)
{
return new HttpStatusCodeResult((int)HttpStatusCode.Forbidden);
}
else
{
return HttpUnauthorized();
}
}
return HttpUnauthorized();
}
在 Startup.cs 文件中,我尝试过以下变化:
services.Configure<CookieAuthenticationOptions>(o =>
{
o.LoginPath = PathString.Empty;
o.ReturnUrlParameter = PathString.Empty;
o.AutomaticChallenge = false;
});
每当登录失败(请忽略返回的密码),应该会出现一个空的401页面,但我却被重定向到/Account/Login。这里有什么诀窍吗?