尝试将mvc登录页面组合在一起,该页面将重定向回原始页面,该页面需要基于其他堆栈溢出帖子的授权。
使用
重定向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”。我应该改变什么来使这项工作?
答案 0 :(得分:2)
原来所有这些代码都是正确的。但是,我没有把
[RequireHttps(Order=0)]
在我的控制器上,所以它永远不会正确存储cookie,并在重定向回我的索引页面之后再次失败授权。
答案 1 :(得分:0)
我正在使用表单身份验证,但我使用此行..
的Response.Redirect(!string.IsNullOrEmpty(RETURNURL) ? returnUrl: FormsAuthentication.DefaultUrl);