I am writing an asp.net MVC 2.0 application and I need to get the username after the user logs in and pass it to other functions. I tried this by simply changing the standard LogOn method for AccountController, here is my code:
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (MembershipService.ValidateUser(model.UserName, model.Password))
{
FormsService.SignIn(model.UserName, model.RememberMe);
using (ShopController newCtrl = new ShopController())
{
if (Session["TempCart"] != null)
{
newCtrl.CreateShoppingCartFromSession((Cart)Session["TempCart"], User.Identity.Name);
}
}
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
return View(model);
}
The problem is that even if the user is successfully registered, "User.Identity.Name" is empty, and I do not know why ...
Any help would be greatly appreciated.
source
share