我的目标是从登录控制器返回401错误。我用内置的.Net会员提供商启动了一个股票MVC网站。目前发生的是,当我从我的控制器返回401时,我得到http://xxxxx/Account/Login?ReturnURL=XXXX
我不希望重定向发生,我只是希望控制器返回401,以便我可以在客户端处理它。任何有关此问题的帮助将不胜感激。
除了帐户控制器上的LOGIN方法
之外,所有代码都是库存会员提供商[HttpPost]
[AllowAnonymous]
public async Task<ActionResult> Login(LoginViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, change to shouldLockout: true
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
return new HttpStatusCodeResult(200);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
// return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
return new HttpStatusCodeResult(401);
}
}
答案 0 :(得分:0)
在MVC5中,项目中有Startup.cs
,代码为
using Microsoft.Owin;
using Owin;
[assembly: OwinStartupAttribute(typeof(WebMVC.Startup))]
namespace WebMVC
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
}
}
注释掉ConfigureAuth(app);
并且它不会在Startup.Auth.cs
中注册该函数,因此您可以获得HTTP错误401.0 - 未经授权
答案 1 :(得分:-1)
我没有遵循mIchaelMao的建议。我必须在Startup.Auth.cs
的第28行注释掉LoginPath = new PathString("/Account/Login")