MVC 4 SimpleMembership - почему WebSecurity.CurrentUserId -1 после входа в систему
Я пытаюсь установить cookie при входе в систему и у меня возникают проблемы с получением текущего идентификатора пользователя после входа в систему. Ниже intUserId равен -1, а WebSecurity.IsAuthenticated имеет значение false. Разве это не правильное место, чтобы поставить этот код? После этого он перенаправляет на домашнюю страницу ... поэтому не уверен, почему это не то место.
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
//TODO: set current company and facility based on those this user has access to
int intUserId = WebSecurity.GetUserId(User.Identity.Name);
int intUserId2 = WebSecurity.GetUserId(model.UserName);
UserSessionPreferences.CurrentCompanyId = 1;
UserSessionPreferences.CurrentFacilityId = 1;
return RedirectToLocal(returnUrl);
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}