I am building an MVC 5 application and have come to the following problem: I want to show a menu item to the user, after the user has logged in, if the user has an Agreement with me.
I want to set a session variable at the moment the user logs in like:
Session["HasAgreement"] = Agreement.HasAgreement(userId);
and then in my _Layout.cshtml file where I build my menu do something like:
@if (Session["HasAgreement"] == "True")
{
<li>@Html.ActionLink("Agreement", "Agreement", "Home")</li>
}
My problem arises in the AccountController where I have added the logic to the standard Login Action:
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
var userId = User.Identity.GetUserId();
Session["HasAgreement"] = Agreement.HasAgreement(userId);
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
This is the standard MVC 5 login - except that I have added the two lines right after the "case SignInStatus.Success:" where I try to get the userId and then set the Session variable.
My problem is that at this point in thime the User is not authenticated(I thought that happened in the SignInManager above).
How do I set the session variable right after the user logs in?