【问题标题】:ASP.Net MVC 4 Custom Authorization Ticket Redirect IssueASP.Net MVC 4 自定义授权票证重定向问题
【发布时间】:2012-11-07 19:00:34
【问题描述】:

我在设置自定义表单身份验证票证后重定向到安全操作时遇到问题。以下是正在发生的事情:

  1. 我导航到站点/主页/索引
  2. 我被自动重定向到站点/帐户/登录
  3. 我使用有效用户/密码登录
  4. RedirecToUrl() 函数尝试将我重定向回站点/主页/索引,但我会自动返回站点/帐户/登录
  5. 请求已通过身份验证。如果我手动导航到站点/主页/索引,则可以进入。

有人能解释一下吗?

我的家庭控制器:

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

我的帐户控制器:

    [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");
    }

这是我创建自定义票证的方式(仅添加用户数据):

    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);
    }

【问题讨论】:

    标签: asp.net-mvc forms-authentication formsauthenticationticket


    【解决方案1】:

    啊!!!

     RedirectToUrl(returnUrl);
    

    需要

     return RedirectToUrl(returnUrl);
    

    【讨论】:

      猜你喜欢
      • 2010-11-01
      • 2013-12-14
      • 2013-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-30
      相关资源
      最近更新 更多