我正在尝试通过 Web API 2 登录。我所做的是从 MVC 5.2 的 AccountController 复制一些代码,但是当我运行此代码时,ApplicationSignInManager 是null并且它抛出异常。
我在 Github 或 ASP.NET 上找不到任何示例项目。
这是我的自定义 UserController Web API。
[RoutePrefix("api/users")]
public class UserController : ApiController
{
... // Truncated for brevity
[Route("login")]
[HttpPost]
public HttpResponseMessage Login(LoginViewModel model)
{
if (!ModelState.IsValid)
{
return new HttpResponseMessage(HttpStatusCode.Forbidden);
}
var signinManager = Request.GetOwinContext().Get<ApplicationSignInManager>();
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, change to shouldLockout: true
SignInStatus result =
signinManager.PasswordSignIn(model.Email, model.Password, model.RememberMe, false);
switch (result)
{
case SignInStatus.Success:
return new HttpResponseMessage(HttpStatusCode.OK);
default:
return new HttpResponseMessage(HttpStatusCode.Forbidden);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?