已经登录Mvc3时重定向登录页面的问题

时间:2013-10-08 07:40:49

标签: asp.net-mvc-3 authorization roles

我很难尝试解决这个问题。当我输入用户名和密码时,我总是在登录页面中重定向。为什么会这样呢???

这是我的登录代码

帐户管理员:

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                     if (Roles.IsUserInRole("Employer"))
                    {
                        return RedirectToAction("CustomerIndex", "Customer");
                    }
                     else if (Roles.IsUserInRole("Worker"))
                    {
                        return RedirectToAction("WorkerIndex", "Worker");
                    }
                     else if (Roles.IsUserInRole("Administrator"))
                     {
                         return RedirectToAction("ClientIndex", "Client");
                     }
                     else 
                     {
                         return View(model);
                     }

                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

感谢那些愿意提供帮助的人:)

1 个答案:

答案 0 :(得分:1)

查看代码时,有三种情况可能发生:

  1. 模型验证失败 - >例如,用户未提供用户名
  2. 用户提供的凭据不正确,ValidateUser方法返回false
  3. 凭据验证成功,但用户未处于任何角色EmployerWorkerAdministrator
  4. 在您的代码中放置一个断点,以查看您的情况并采取相应措施。