Using the ASP.NET MVC 3 Logon returnUrl Parameter

razor:

@{
  ViewBag.Title = "登陆";
  Layout = "~/Views/Shared/_LoginLayout.cshtml";
  string retUrl = "";
  if (Request["ReturnUrl"] != null)
  {
    retUrl = ViewContext.HttpContext.Request["ReturnUrl"];
  }
}
@using (Html.BeginForm("Logon", "Account", new { model = this.Model, ReturnUrl = retUrl }))
{
//form
}

controller:

[HttpPost]
public ActionResult LogOn(LogOnModel model, string ReturnUrl)
{
  ViewBag.UserLogOut = true;
  if (!ModelState.IsValid)
    return View("Logon");
  if (ModelState.IsValid)
  {
    try
    {
      //login
    }
    catch (Exception e)
    {
      ModelState.AddModelError("", e.Message);
      return View(model);
    }
  }
  if (!string.IsNullOrEmpty(ReturnUrl))
    return Redirect(ReturnUrl);//return
  return RedirectToAction("Index", "Product");
}

UserSessionAuthorizeAttribute:

public class UserSessionAuthorizeAttribute : AuthorizeAttribute
{
  public override void OnAuthorization(AuthorizationContext  filterContext)
  {
    base.OnAuthorization(filterContext);
    if(//check session)
    {
      filterContext.Result = new RedirectToRouteResult(
        new System.Web.Routing.RouteValueDictionary
            {
              { "controller", "Account" },
              { "action", "LogOn" },
              { "ReturnUrl", filterContext.HttpContext.Request.RawUrl }
            });
    }
  }
}

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注