Saturday, 31 August 2013

MVC4 WebSecurity.Login fails first time through, can't get User in Login method

MVC4 WebSecurity.Login fails first time through, can't get User in Login
method

Ok, an afternoon down the drain, I concede to SO assistance. My login
method is pretty standard. As shown below, I'm using WebSecurity.Login.
Right after that, I want to run a check to see if the user's profile is
completed and if it isn't, send them over to that view.
Controller
[AllowAnonymous]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.Email,
model.Password, persistCookie: model.RememberMe))
{
if (!User.IsInRole("User"))
{
if
(!IsProfileComplete(WebSecurity.GetUserId(User.Identity.Name)))
{
return RedirectToAction("ProfileCompletion");
}
if(!User.IsInRole("User"))
Roles.AddUserToRole(User.Identity.Name, "Vendor");
}
else // user has role
{
return RedirectToAction("Index", "Dashboard");
}
}
ModelState.AddModelError("", "Unknown username or password.");
return View(model);
}
I've stepped through it a bunch....after WebSecurity.Login I was under the
impression the WebSecurity user data would get set but
WebSecurity.GetUserId(User.Identity.Name) comes back as -1
Is there something about the http post and login context set that I'm
missing? The end result I am looking for is to just make sure a user has
completed the profile page before let into the system. maybe there is a
better way to do this?

No comments:

Post a Comment