ASP.NET MVC 4自定义授权机票重定向问题



我遇到了一个问题,在设置自定义表单身份验证票证后,将其重定向到安全措施。这是发生的事情:

  1. 我导航到网站/家庭/索引
  2. 我会自动重定向到网站/帐户/登录
  3. 我使用有效的用户登录
  4. redirectourl()函数试图将我重定向回到网站/家庭/索引,但我会自动返回站点/帐户/ligin
  5. 该请求已进行身份验证。如果我手动导航到网站/家庭/索引,我允许。

任何人都可以脱光灯吗?

我的homecontroller:

[Authorize]
public ActionResult Index()
{
    return View();
}

我的帐户controller:

    [HttpGet]
    [AllowAnonymous]
    public ActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;
        return View();
    }
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Login(LoginModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            bool bLogin = MyAuthentication.Login(model.UserName, model.Password);
            if (bLogin)
            {
                Response.Cookies.Add(MyAuthentication.GetAuthenticationCookie(model.UserName.ToLower(), model.RememberMe));
                RedirectToUrl(returnUrl);
            }
            else
                ModelState.AddModelError("", "That is not a valid Username/Password combination");
        }
        return View(model);
    }
    private ActionResult RedirectToUrl(string returnUrl)
    {
        if (Url.IsLocalUrl(returnUrl))
            return Redirect(returnUrl);
        else
            return RedirectToAction("Index", "Home");
    }

这是我创建自定义票(只添加userData)的方式:

    public static HttpCookie GetAuthenticationCookie(string UserName, bool persistLogin)
    {
        var userData = null; // Code removed for brevity
        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
                 1,
                 UserName,
                 DateTime.Now,
                 DateTime.Now.AddMinutes(20),
                 persistLogin,
                 userData);
        string encTicket = FormsAuthentication.Encrypt(authTicket);
        return new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
    }

ugh !!!

 RedirectToUrl(returnUrl);

需要

 return RedirectToUrl(returnUrl);

最新更新