ASP.NET MVC - 使用洛外部网站在用户CustomeAuthorize筛选器操作 [英] ASP.NET MVC - CustomeAuthorize filter action using an external website for loggin in the user
问题描述
我有一个CustomeAuthorize动作过滤器转发该用户,如果用户没有通过验证到登录页面。我将此过滤器应用于行动或控制器。
I have a CustomeAuthorize action filter that forwards the user to signin page if user is not authenticated. I apply this filter to actions or controllers.
[CustumeAuthorize]
public ActionResult MyAction()
{
//do something here
return View();
}
和过滤器看起来像这样:
and the filter looks like this:
public class CustomAuthorizeAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!currentUserIsAuthenticated)
{
filterContext.Result =
new RedirectToRouteResult(
new RouteValueDictionary{{ "controller", "Account" },
{ "action", "SignIn" },
{ "returnUrl", filterContext.HttpContext.Request.RawUrl }
});
}
else
{
base.OnActionExecuting(filterContext);
}
}
}
在我的值赋给filterContext.Result,过滤器完成执行后,执行是(不知何故?!)重定向到签到行动,MyAction不执行。这正是我想要的。
Once I assign a value to filterContext.Result, after execution of filter finishes, the execution is (somehow?!) redirected to the SignIn action and MyAction does not execute. This is exactly what I want.
现在说我要改变我的CustomAuthorize用户对外部网站,而不是我自己的签到行为验证,所以我做这样的事情:
Now say I want to change my CustomAuthorize to authenticate the user against an external website and not my own SignIn action so I am doing something like this:
public class CustomAuthorizeAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!currentUserIsAuthenticated)
{
filterContext.HttpContext.Response.Redirect("http://externalSite.com/login?returnUrl=" + filterContext.HttpContext.Request.RawUrl);
}
else
{
base.OnActionExecuting(filterContext);
}
}
}
我的问题是的的CustomAuthorize过滤器的第二个版本执行完成后,继续执行到MyAction这不是我想要的!如何在这种情况下,过滤器后停止MyAction执行?
My problem is that after the execution of the second version of CustomAuthorize filter is finished, execution continues to MyAction which is not what I want! How do I stop the execution of MyAction after filter in this case?
- 更新 - 我只是碰到了新的问题来了。我的MVC应用程序是在一个iFrame,我想重定向到强制将当前帧重定向后的主框架,所以我做这样的事情:
-Update- I just came across a new issue. My MVC application is in an iFrame and I want the Redirect to force the current frame as the main frame after redirection, so I am doing something like:
string url = "http://externalSite.com/login?returnUrl=" + filterContext.HttpContext.Request.RawUrl;
filterContext.HttpContext.Response.Write("<script type=\"text/javascript\">\ntop.location.href = \"" + url + "\";</script>");
有没有办法通过一个JavaScript来RedirectResult()?
Is there a way to pass a javascript to RedirectResult()?
推荐答案
使用类似于你如何使用RedirectToRouteResult之前更换,结果在过滤背景下RedirectResult。
Use the RedirectResult similar to how you were using the RedirectToRouteResult before to replace the result in the filter context.
filterContext.Result = new RedirectResult("http://externalSite.com/login?returnUrl=" + filterContext.HttpContext.Request.RawUrl );
这篇关于ASP.NET MVC - 使用洛外部网站在用户CustomeAuthorize筛选器操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!