In my MVC5 ASP.NET C# application, I want to store some important but temporary data in the session. For that I tried to set the session on receipt of signin success as following:-
public class AccountController: Controller
{
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
.
.
var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.rememberMe, shouldLockout= false);
switch (result){
case SignInStatus.Success:
var user = UserManager.FindById(User.Identity.GetUserId());
return RedirectToLocal(returnUrl);
.
.
}
}
whilst this one fails , calling User.Identity.GetUserId() inside the HomeController return the correct result.
Can anyone please explain why this happens and how can get the Current user after Signing in so as to set the session.
UPDATE
I have now retrieved the User object by using method UserManager.FindBynameAsync(model.UserName). However I still cannot figure out why the UserId is not available despite of SignIn success.