【发布时间】:2016-04-18 16:12:06
【问题描述】:
当我从控制器返回 NotAuthorized IActionResult 时,我一直试图阻止重定向,但无论我如何尝试,NotAuthorized 都会被转换为重定向。
我已经尝试过提到的here(同样的问题,使用较旧的 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;
});
每次登录失败(请忽略密码返回到 Ok)并且应该导致一个空的 401 页面时,我会重定向到 /Account/Login。这里有什么诀窍?
【问题讨论】:
-
您是否尝试在登录页面上执行此操作?还是用于 REST API 身份验证?如果它是一个 API,你可能会看这个:wildermuth.com/2015/9/10/ASP_NET_5_Identity_and_REST_APIs
-
REST API,您的链接假定 Notifications 命名空间存在,但已被删除。链接的解决方案不再起作用。
标签: redirect asp.net-core identity asp.net-core-mvc unauthorized