asp.net-mvc – ASP.NET MVC – CustomeAuthorize使用外部网站对
发布时间:2020-12-16 00:04:13 所属栏目:asp.Net 来源:网络整理
导读:我有一个CustomeAuthorize操作过滤器,如果用户未经过身份验证,则会将用户转发到登录页面.我将此过滤器应用于操作或控制器. [CustumeAuthorize]public ActionResult MyAction(){ //do something here return View();} 过滤器看起来像这样: public class Cust
我有一个CustomeAuthorize操作过滤器,如果用户未经过身份验证,则会将用户转发到登录页面.我将此过滤器应用于操作或控制器.
[CustumeAuthorize] public ActionResult MyAction() { //do something here return View(); } 过滤器看起来像这样: 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赋值,在执行过滤器完成后,执行(以某种方式?!)重定向到SignIn操作,MyAction不执行.这正是我想要的. 现在说我想改变我的CustomAuthorize来对外部网站进行身份验证,而不是我自己的SignIn操作,所以我做的是这样的: 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? – 更新 – 我刚刚遇到一个新问题.我的MVC应用程序在iFrame中,我希望Redirect在重定向后强制当前帧作为主帧,所以我做的事情如下: 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()? 解决方法
使用RedirectResult类似于之前使用RedirectToRouteResult的方式来替换过滤器上下文中的结果.
filterContext.Result = new RedirectResult("http://externalSite.com/login?returnUrl=" + filterContext.HttpContext.Request.RawUrl ); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-4 – 如何输出Request.CreateResponse方法的js
- asp.net-mvc – 尝试方法… HttpConfiguration …访问方法…
- asp.net – 为什么我的控件获得不同的id命名约定?
- asp.net – 中继器中继器
- asp.net-mvc-4 – 如果使用实体连接字符串(与SQL连接字符串
- asp.net – 在ashx文件中,我可以设置返回文件的文件名吗?
- asp.net-mvc – 更改kendo数字过滤器格式
- asp.net – 仅在本地IIS服务器上支持创建虚拟目录
- asp.net – PreviousPage =在网页发布中为null
- ASP.NET异步任务 – 如何使用Page.RegisterAsyncTask使用We
推荐文章
站长推荐
- asp.net-mvc – MVC中的错误处理
- asp.net-mvc – MVC简单模型绑定无法正常工作
- 将azure AD身份验证与asp.net核心身份个人帐户集
- asp.net – 我什么时候应该使用Webservice属性Sy
- asp.net-mvc – 从数据库显示剃须刀
- asp.net – 当浏览器在HTTP Request标头中设置“
- asp.net-mvc – 具有Repository,Service Layer和
- 实体框架 – 使用Azure表存储的Code First&I
- 编写T4模板进行代码生成无法避免的两个话题:
- asp.net-mvc – 如何添加“必需”属性到mvc 5剃刀
热点阅读