MVC 4为什么我的网站会将我重定向到帐户/登录?

时间:2016-09-07 08:39:23

标签: c# asp.net asp.net-mvc asp.net-mvc-4 redirect

我的计划以前工作过。我不知道我做了什么改变,但现在突然我的登录表现得如此奇怪。每次我尝试访问管理员授权页面时,它都会将我重定向到登录页面,即使在我登录后也是如此。这是我的代码:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Login(AlvinCMSExtension.Models.LoginModel model, string returnUrl)
    {
        string redirectUrl = returnUrl;
        string userName = model.UserName;
        AlvinCMSExtension.Models.UserProfile user = dbAccount.UserProfiles.Where(m => m.Email.Equals(userName, StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault();
        if (user != null)
        {
            userName = user.UserName;
        }

        if (ModelState.IsValid && WebSecurity.Login(userName, model.Password, persistCookie: model.RememberMe))
        {
            return RedirectToAction("LoginRedirectionControl", new { redirectUrl = redirectUrl });
        }
        // If we got this far, something failed, redisplay form
        ModelState.AddModelError("", "The user name or password provided is incorrect.");
        return View(model);
    }

    public ActionResult LoginRedirectionControl(string redirectUrl)
    {
        string returnUrl = redirectUrl;
        if (redirectUrl == null)
        {
            redirectUrl = User.IsInRole("Admin") ? "/Admin" : "/";
        }
        return RedirectToLocal(redirectUrl);
    }

    private ActionResult RedirectToLocal(string returnUrl)
    {
        if (Url.IsLocalUrl(returnUrl))
        {
            return Redirect(returnUrl);
        }

        return RedirectToAction("Home", "Page");

    }

我试图访问它:

    [Authorize(Roles="Admin")]
    public ActionResult Dashboard()
    {
        return View();
    }

每次成功登录后,Redirect(returnUrl)都不会将我带到returnUrl,而是再次登录页面。使用的参数是:http://localhost:5847/Account/Login?ReturnUrl=%2fAdmin%2fDashboard。我调试代码,returnUrl持有/Admin/Dashboard/。我不知道发生了什么。

1 个答案:

答案 0 :(得分:3)

检查用户是否具有“管理员”角色,可能会将其删除