如何解决用户在成功后重定向到登录的问题,而不是用户页面?

时间:2014-01-08 21:55:32

标签: asp.net-mvc-4 c#-4.0 custom-membershipprovider

在MVC4中,我创建了一个自定义成员资格提供程序,如果用户身份验证通过,则返回true。这里没什么大不了的 - 这个部分按照应有的方式运作:

    public override bool ValidateUser(string username, string password)
    {
        var crypto = new SimpleCrypto.PBKDF2(); // type of encryption
        // TODO: using (var unitOfWork = new Website.Repository.UnitOfWork(_dbContext))
        //var unitOfWork1 = new Website.Repository.UnitOfWork(_dbContext);

        using (var db = new Website.DAL.WebsiteDbContext())
        {
            var user = db.Users
                .Include("MembershipType")
                .FirstOrDefault(u => u.UserName == username);
            if (user != null && user.Password == crypto.Compute(password, user.PasswordSalt))
            {
                FormsAuthentication.SetAuthCookie(username, true);
                return true;
            }
        }
        return false;
    }

在我的登录操作中:

    [HttpPost]
    [AllowAnonymous]
    public ActionResult Login(Models.UserModel user)
    {
        if (ModelState.IsValid)
        {
            // custom membership provider
            if (Membership.ValidateUser(user.UserName, user.Password))
            {
                // Cannot use this block as user needs to login twice
                //if (User.IsInRole("WaitConfirmation"))  // checks the custom role provider and caches based on web.config settings
                //{
                //    //TempData["EmailAddress"] = thisUser.Email;

                //    // email address has not yet been confirmed
                //    return RedirectToAction("WaitConfirmation");
                //    //return View("Account", thisUser)
                //}
                //else
                //{
                //    // get custom identity - user properties
                //    string userName = UserContext.Identity.Name;
                //    //CustomIdentity identity = (CustomIdentity)User.Identity;
                //    var identity = UserContext.Identity;
                //    int userId = identity.UserId;

                //    return RedirectToAction("Index", "Dashboard");
                //}

                if (User.Identity.IsAuthenticated && User.IsInRole("WaitConfirmation"))  // checks the custom role provider and caches based on web.config settings
                {
                    return RedirectToAction("WaitConfirmation");
                }
                else if (User.Identity.IsAuthenticated)
                {
                    // get custom identity - user properties
                    string userName = UserContext.Identity.Name;

                    return RedirectToAction("Index", "Dashboard");
                }
            }
            else
            {
                ModelState.AddModelError("", "Login data is incorrect.");
            }
        }

        return View(user);
    }

在单步执行代码时,当用户首次登录时,User.Identity.IsAuthenticated为false,页面将重定向回登录页面。在这一点上,如果我:

  • 手动导航到用户页面(仪表板),用户的详细信息可用
  • 再次登录,此作品

我认为答案在于User.Identity.IsAuthenticated不是立即true的原因,但是第一次无法弄清楚这是错误的。

由于没有Unable to cast object of type 'System.Security.Principal.GenericIdentity' to type 'Website.AdminWebsite.Infrastructure.CustomIdentity'检查,第一个已注释掉的代码块因IsAuthenticated而失败。

建议?

2 个答案:

答案 0 :(得分:1)

这篇文章描述了类似症状的问题。

http://forums.asp.net/t/1177741.aspx

请阅读并确保您的活动顺序(即Authenticate,LoggedIn)

答案 1 :(得分:1)

在阅读@ mcsilvio建议的文章后,我添加了一个RedirectToAction(),如下所示,以启动新的页面生命周期:

    public ActionResult Login(Models.UserModel user)
    {
        if (ModelState.IsValid)
        {
            // custom membership provider
            if (Membership.ValidateUser(user.UserName, user.Password))
            {
                return RedirectToAction("VerifyIdentity", user);
            }
            else
            {
                ModelState.AddModelError("", "Login data is incorrect.");
            }
        }

        return View(user);
    }

    public ActionResult VerifyIdentity(Models.UserModel user)
    {
        if (User.Identity.IsAuthenticated && User.IsInRole("WaitConfirmation"))  // checks the custom role provider and caches based on web.config settings
        {
            return RedirectToAction("WaitConfirmation");
        }
        else if (User.Identity.IsAuthenticated)
        {
            // get custom identity - user properties
            string userName = UserContext.Identity.Name;

            return RedirectToAction("Index", "Dashboard");
        }

        return View(User);
    }

这就是诀窍,但我想知道是否有更好的方法,或者总是这样做?