为什么登录后user.Identity.Name为空?Asp.net mvc 2.0

dom*_*ren 3 session asp.net-mvc-2

我正在编写一个asp.net MVC 2.0应用程序,我需要在用户登录后获取用户名并将其传递给其他函数.我尝试通过简单地修改AccountController的标准LogOn方法,这是我的代码:

[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);
    }
Run Code Online (Sandbox Code Playgroud)

问题是即使用户成功登录,"User.Identity.Name"为空,我不知道为什么......

任何帮助将非常感激.

Car*_*lis 6

表单身份验证方法设置了一个身份验证cookie,该cookie在下一个请求中首次被读取和处理,因此User.Identity只能从下一个请求获得.

在这种方法中,您必须先进行重定向或使用您的model.UserName.