Using ApplicationSignInManager in WEB API 2

I am trying to log in via Web API 2. What I did was copy some code from AccountController from MVC 5.2, but when I run this code, ApplicationSignInManager nulland it throws an exception.

I can not find sample projects on Github or ASP.NET.

This is my UserController web user 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);
        }
    }
}

What am I doing wrong?

+4
source share

All Articles