asp.net-mvc – “安全感知”动作链接?
发布时间:2020-12-15 18:31:54 所属栏目:asp.Net 来源:网络整理
导读:如何创建一个“安全感知”操作链接来检测用户是否有权点击(调用)操作? 如果用户不允许使用该操作,请隐藏链接… 取决于 web.config(授权)和 [Authorize]属性操作 PS 我想在MVC中混合这两个是不好的做法? 解决方法 这是从MvcSitemap项目中偷取的一些代码,
如何创建一个“安全感知”操作链接来检测用户是否有权点击(调用)操作?
如果用户不允许使用该操作,请隐藏链接… 取决于 > web.config(授权)和 PS 解决方法
这是从MvcSitemap项目中偷取的一些代码,并修改为我自己的使用。如果我记得这个代码已被修改为MVC2,并且某些功能可能必须被重新移植到MVC1。
将MVC和FormsAuthentication混合在一起的不错的做法,MVC的默认身份验证方法围绕现有的Asp.net安全基础架构构建。 确定用户是否具有权限的代码: public static class SecurityTrimmingExtensions { public static bool HasActionPermission( this HtmlHelper htmlHelper,string actionName,string controllerName ) { //if the controller name is empty the ASP.NET convention is: //"we are linking to a different controller ControllerBase controllerToLinkTo = string.IsNullOrEmpty(controllerName) ? htmlHelper.ViewContext.Controller : GetControllerByName(htmlHelper,controllerName); var controllerContext = new ControllerContext(htmlHelper.ViewContext.RequestContext,controllerToLinkTo); var controllerDescriptor = new ReflectedControllerDescriptor(controllerToLinkTo.GetType()); var actionDescriptor = controllerDescriptor.FindAction(controllerContext,actionName); return ActionIsAuthorized(controllerContext,actionDescriptor); } private static bool ActionIsAuthorized(ControllerContext controllerContext,ActionDescriptor actionDescriptor) { if (actionDescriptor == null) return false; // action does not exist so say yes - should we authorise this?! AuthorizationContext authContext = new AuthorizationContext(controllerContext); // run each auth filter until on fails // performance could be improved by some caching foreach (IAuthorizationFilter authFilter in actionDescriptor.GetFilters().AuthorizationFilters) { authFilter.OnAuthorization(authContext); if (authContext.Result != null) return false; } return true; } private static ControllerBase GetControllerByName(HtmlHelper helper,string controllerName) { // Instantiate the controller and call Execute IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory(); IController controller = factory.CreateController(helper.ViewContext.RequestContext,controllerName); if (controller == null) { throw new InvalidOperationException( String.Format( CultureInfo.CurrentUICulture,"Controller factory {0} controller {1} returned null",factory.GetType(),controllerName)); } return (ControllerBase)controller; } } Html助手 public static class SecurityTrimmedLink { public static MvcHtmlString SecurityTrimmedActionLink(this HtmlHelper htmlHelper,string linkName,string actionName) { return htmlHelper.HasActionPermission(actionName,"") ? htmlHelper.ActionLink(linkName,actionName) : MvcHtmlString.Create(""); } public static MvcHtmlString SecurityTrimmedActionLink(this HtmlHelper htmlHelper,RouteValueDictionary routeValueDictionary ) { return htmlHelper.HasActionPermission(actionName,actionName,routeValueDictionary) : MvcHtmlString.Create(""); } public static MvcHtmlString SecurityTrimmedActionLink(this HtmlHelper htmlHelper,object routeValues,object htmlAttributes ) { return htmlHelper.HasActionPermission(actionName,routeValues,htmlAttributes) : MvcHtmlString.Create(""); } public static MvcHtmlString SecurityTrimmedActionLink(this HtmlHelper htmlHelper,string controllerName) { return htmlHelper.HasActionPermission(actionName,controllerName) ? htmlHelper.ActionLink(linkName,controllerName) : MvcHtmlString.Create(""); } public static MvcHtmlString SecurityTrimmedActionLink(this HtmlHelper htmlHelper,string controllerName,object htmlAttributes) { return htmlHelper.HasActionPermission(actionName,controllerName,htmlAttributes) : MvcHtmlString.Create(""); } } 警告:这不会在MVC 5中工作,因为调用FindAction()不会返回一个动作描述符 我试图找到这个问题,不能完成编程工作。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 使用web api和asp.net mvc 5进行单点登录
- asp.net-mvc – NGEN可以与Azure网站一起使用吗?
- CanvasPath Schema
- asp.net-mvc – 使用MVC3剃刀的ASP.Net图表控件
- 是否可以在IIS 7中将虚拟目录设置为根目录?
- asp.net – 什么条件导致Web浏览器显示“Page Expired”消息
- 用HttpClientFactory来实现简单的熔断降级
- asp.net – IIS如何知道它是服务于一个网站还是一个Web应用
- asp.net-mvc – 在Web Api 2中更改IdentityUser表的模式
- 将ASP.NET成员资格表添加到我自己的现有数据库中,还是应该配
推荐文章
站长推荐
- asp.net-mvc-3 – 检查ViewBag是否具有属性,以有
- asp.net – 有没有一个原因,cshtml不受欢迎
- asp.net-mvc-3 – 具有确认对话框的MVC3 Actionl
- asp.net-mvc-3 – asp.net mvc的telerik grid的开
- asp.net-mvc-3 – Mini Profiler不渲染脚本
- asp.net-web-api – OWIN中间件中的全局异常处理
- asp.net-mvc-3 – 检查.NET MVC中的AngularJS $资
- asp.net – 多个域,IIS 6上的相同Web应用程序
- asp.net-mvc-3 – .NET MVC 3以编程方式设置布局
- 如何从经典ASP输出Excel * .xls文件
热点阅读