MVC3登录后重定向
问题描述:
一直试图放置一个mvc登录页面,该页面将重定向回到基于其他堆栈溢出帖子需要授权的原始页面。MVC3登录后重定向
重定向与
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{
{ "action", "login" },
{"ReturnUrl", filterContext.HttpContext.Request.RawUrl},
{ "controller", "mycontroller" }
});
}
登录页面视图具有
@Html.Hidden("returnUrl", ViewContext.HttpContext.Request.Params["returnurl"])
位于形式的内部。我最初使用ViewContext.HttpContext.Request.Url.PathAndQuery基于另一个论坛帖子,但包括整个网址。
内部控制器我有
[HttpPost]
public ActionResult Login(MyModel model)
{
if (loginisValid)
return Redirect(model.ReturnUrl);
else
return View();
}
但什么都不做,只是返回即使它击中重定向页面一部开拓创新。我也尝试了RedirectToAction和RedirectToRoute,但是那些给出了错误。我在我的索引页上测试这是在localhost/admin/mycontroller。所以model.ReturnUrl目前包含“/ admin/mycontroller”。我应该改变什么来完成这项工作?
答
原来所有这些代码都是正确的。但是,我并没有把
[RequireHttps(Order=0)]
在我的控制器的顶部,以便它永远不会正确重定向回我的索引页它再次失败授权之后存储的cookie。
答
我使用窗体身份验证,但我用这条线..
的Response.Redirect(string.IsNullOrEmpty(RETURNURL) RETURNURL!? FormsAuthentication.DefaultUrl);