Why is user.Identity.Name empty after login? Asp.net mvc 2.0

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.");
            }
        }

        // If we got this far, something failed, redisplay form
        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.

+3
source share
1 answer

cookie , , User.Identity .

, model.UserName.

+6

Source: https://habr.com/ru/post/1767038/


All Articles