[翻译] ASP.NET MVC Framework控制器操作安全性

原文地址:http://gridviewguy.com/Articles/385_ASP_NET_MVC_Framework_Controller_Action_Security.aspx

翻译:Anders Liu

摘要:ASP.NET MVC Framework允许开发者使用更为灵活的方式创建Web应用程序。使用MVC框架可以摆脱令人头疼的ViewState和Postback,还能让应用程序便于测试。在这篇文章中,我们将研究控制器操作的基于角色的安全性。

Introduction
简介

ASP.NET MVC Framework allows the developers to build their web application in a more flexible way. Using MVC framework you by passes the headaches of ViewState and Postbacks and also enable your application for testing. In this article we are going to take a look at the Controller Action Role-Based Security.

ASP.NET MVC Framework允许开发者使用更为灵活的方式创建Web应用程序。使用MVC框架可以摆脱令人头疼的ViewState和Postback,还能让应用程序便于测试。在这篇文章中,我们将研究控制器操作的基于角色的安全性。

Prerequisite
前提

If this is your first encounter with ASP.NET MVC Framework then I strongly suggest that you check out the introductory article using the link below:

如果你第一次接触ASP.NET MVC Framework,我强烈建议你通过下面的链接看一下对它的介绍:

Getting Started with the ASP.NET MVC Framework

Scenario
场景

The scenario is really simple. A list of categories is displayed on the page and when the user clicks on the category it will be deleted. But we need to make sure that the user is authorized to delete the items.

这个场景非常简单。页面上会显示一系列分类,当用户单击分类时,对应的分类会被删除。但我们需要确保用户已被授权,能够删除其中的项。

Populating the Page with List of Categories
生成分类列表页面

The first task is to populate the page with a list of categories. Let’s see how this can be implemented.

第一个任务是生成包含分类列表的页面。我们看看这是如何实现的。

[ControllerAction] public void List() { NorthwindDataContext northwind = new NorthwindDataContext(); var list = northwind.Categories; RenderView("Categories", list); }

The List action is responsible for populating the Categories view with the required data. Let’s check out the Categories view.

List操作负责生成显示所需数据的Categories视图。我们来看一下Categories视图。

public partial class Categories : ViewPage> { } (c => c.Delete(category.id), category.CategoryName, new { onclick = "return confirmDelete(" + category.id + ")" })%>

The first thing to note is the Categories class inherits from the ViewPage which is of IEnumerable<Category> type. This means that we will have the strong type support for IEnumerable<Category> in the HTML view of the page. Now, let’s discuss the HTML part of the Categories view.

第一个要注意的是Categories类继承自用于IEnumerable<Category>的ViewPage类。这意味着在页面的HTML视图中,我们将得到对IEnumerable<Category>的强类型支持。接下来,我们讨论Categories视图的HTML部分。

The foreach loop is used to iterate through the categories. The Html.ActionLink method is used to create hyperlinks which are directed to particular controllers. The first argument to the Html.ActionLink is the Linq expression for the action. The argument c => c.Delete(category.id) means that we are attaching the Delete action to all the categories in the ViewData object. The Delete operation will take a single parameter which is categoryId. The next parameter is the text to appear for the hyperlink. The final parameter is the HTML attributes. We are using onclick attribute of the hyperlink which will popup a confirmation box.

foreach循环用于迭代分类。Html.ActionLink方法用于创建指向特定控制器的超链接。传给Html.ActionLink的第一个参数是与其操作对应的Linq表达式。参数c => c.Delete(category.id)表示附加到ViewData对象中的所有分类的Delete操作。Delete操作携带一个参数categoryId。下一个参数是要显示成超链接的文字。最后一个参数是HTML属性。我们使用超链接的onclick属性弹出一个确认框。

The HTML generated for the page might look something like this:

为该页面生成的HTML看起来象下面这样:

Beverages Edite
Condiments
Confections
Dairy Products

Now, looking at the URL’s above anyone can easily delete the item by simply copying the URL in the address bar. So, the question is how do we secure the controller actions so only authorized users would be able to delete the items.

现在来看一下上面的URL,任何人都可以通过将URL复制到地址栏来删除其中的项。那么,我们如何来确保控制器操作的安全性,使得只有已授权的用户能够删除其中的项呢?

Controller Action Security
控制器操作安全性

ASP.NET MVC Framework is still in its development phases and there is still a lot on the wish list. Maybe in few months the framework will provide us the flexibility to configure action based security easily.

ASP.NET MVC Framework仍处在开发过程中,目标列表中还有很多东西。也许几个月后这个框架就能为我们带来灵活的、简单的、基于操作的安全性。

For now let’s use another approach to add security to our controller actions. The OnPreAction event is fired before the action is executed and this seems to be an ideal place to authorize the user. You can override the OnPreAction of the controller class but this solution is not scalable since then you will need to override all the controllers for security purposes. A better approach is to introduce a BaseController and override the OnPreAction of the BaseController. All the controllers will derive from the BaseController class instead of the Controller class. And the BaseController will derive from the Controller class.

目前,我们只能通过其他途径为控制器操作添加安全性。OnPreAction事件会在操作执行前触发,看起来是个放置用户授权的好地方。你可以重写控制器类的OnPreAction方法,但这中解决方法不具可伸缩性,因为出于安全的目的你需要重写所有控制器。更好的方法是引入一个BaseController并重写BaseController的OnPreAction方法。所有的控制器都从BaseController继承,而不再是Controller类。而BaseController类是从Controller类继承而来的。

protected override bool OnPreAction(string actionName, System.Reflection.MethodInfo methodInfo) { string controllerName = methodInfo.DeclaringType.Name; if(!IsAuthorized(controllerName,actionName)) throw new SecurityException("not authenticated"); return base.OnPreAction(actionName, methodInfo); }

The IsAuthorized custom method is responsible for performing the actual authorization.

IsAuthorized自定义方法负责执行具体的授权。

private bool IsAuthorized (string controllerName, string actionName) { System.Web.HttpContext context = System.Web.HttpContext.Current; XDocument xDoc = null; if (context.Cache["ControllerActionsSecurity"] == null) { xDoc = XDocument.Load(context.Server.MapPath("~/ControllerActionsSecurity.xml")); context.Cache.Insert("ControllerActionsSecurity",xDoc); } xDoc = (XDocument) context.Cache["ControllerActionsSecurity"]; IEnumerable elements = xDoc.Element("ControllerSecurity").Elements(); var role = (from e in elements where ((string)e.Attribute("controllerName")) == controllerName && ((string)e.Attribute("actionName")) == actionName select new { RoleName = e.Attribute("Roles").Value }).SingleOrDefault(); if (role == null) return true; if (!User.IsInRole(role.RoleName)) return false; return true; }

Nothing too complicated! The authorization details are stored in an XML file called ControllerActionsSecurity.xml. Here are the contents of the file:

一点也不复杂!授权的详细信息存放在一个名为ControllerActionSecurity.xml的XML文件中。下面是该文件的内容:

  • controllerName: The name of the controller
  • actionName: The action of the controller
  • Roles: Authorized roles
  • controllerName——控制器的名字
  • actionName——控制器的操作
  • Roles——授权的角色

If you need to add authorization to a different controller then simply make an entry in the XML file with the appropriate controllerName and the actionName.

如果你需要为另一个控制器添加授权,只许用适当的controllerName和actionName在这个XML文件中创建一个入口即可。

Conclusion
小结

In this article we learned how to authorize the user based on the controller and the action. Hopefully, ASP.NET team will introduce more flexible ways to authorize the users based on their actions.

在这篇文章中,我们学到了如何基于控制器和操作为用户授权。希望ASP.NET团队能够为基于操作的用户授权引入更为灵活的方式。

I hope you liked the article happy coding!

希望你能喜欢这篇文章,编码快乐!

此处下载源代码:http://gridviewguy.com/ArticleDownloads/AspAllianceMVC_a.zip。

转载于:https://www.cnblogs.com/AndersLiu/archive/2008/08/22/ASPNET-MVC-Framework-Controller-Action-Security.html

[翻译] ASP.NET MVC Framework控制器操作安全性相关推荐

  1. [翻译-ASP.NET MVC]Contact Manager开发之旅

    本翻译系列为asp.net mvc官方实例教程.在这个系列中,Stephen Walther将演示如何通过ASP.NET MVC framework结合单元测试.TDD.Ajax.软件设计原则及设计模 ...

  2. ASP.NET MVC Framework体验(1):从一个简单实例开始

    概述 12月10日微软发布了ASP.NET 3.5扩展的预览版,在其中包括了ASP.NET MVC Framework.ASP.NET AJAX改进.ASP.NET动态数据支持.ASP.NET SIl ...

  3. ASP.NET MVC Framework 系列

    序言 做为设计模式的王者,MVC在众多领域都成为良好的模型的代名词,从前在ASP.NET下我们只能依靠Monorail来实现ASP.NET下无控件的MVC,但是现在ASP.NET 下的MVC已经成为现 ...

  4. [翻译-ASP.NET MVC]Contact Manager开发之旅迭代3 - 验证表单

    本翻译系列为asp.net mvc官方实例教程.在这个系列中,Stephen Walther将演示如何通过ASP.NET MVC framework结合单元测试.TDD.Ajax.软件设计原则及设计模 ...

  5. ASP.NET MVC Framework体验(2):显示列表数据

    概述 ASP.NET WebForm下,显示列表数据,经常会使用服务器控件GridView.DataList等.在ASP.NET MVC Framework中,我们有两种方式进行显示数据,一是使用行内 ...

  6. 理解ASP.NET MVC Framework Action Filters(翻的)

    原文地址:Understanding Action Filters 本指南主要解释action filters,action filter作为一个可以应用到controller action(或者是整 ...

  7. .NET/ASP.NET MVC Controller 控制器(IController控制器的创建过程)

    阅读目录: 1.开篇介绍 2.ASP.NETMVC IControllerFactory 控制器工厂接口 3.ASP.NETMVC DefaultControllerFactory 默认控制器工厂 4 ...

  8. ASP.NET MVC编程——控制器

    每一个请求都会经过控制器处理,控制器中的每个方法被称为控制器操作,它处理具体的请求. 1操作输入参数 控制器的操作的输入参数可以是内置类型也可以是自定义类型. 2操作返回结果 结果类型 调用方法 备注 ...

  9. Asp.net Mvc 多级控制器 路由重写 及 多级Views目录 的寻找视图的规则 (多级路由) 如:Admin/Test/Index...

    http://blog.csdn.net/buhuan123/article/details/26387427 目录(?)[-] 1那么我们再来看我们需要的访问方式如下图 razor视图的地址写成通配 ...

  10. (转)[翻译] ASP.NET MVC Tip #1 - 使用扩展方法创建新的HTML Helper

    原文地址:http://weblogs.asp.net/stephenwalther/archive/2008/06/13/asp-net-mvc-tip-1-creating-new-html-he ...

最新文章

  1. GitHub移动端正式发布
  2. oracle维护常用SQL语句(查看系统表和视图)
  3. 《 廊桥遗梦 》:用我的整个余生和全部的心来爱你 ...
  4. export default 打包_贵阳【打包扣】价格
  5. CoreTextHyperlinkView
  6. 快速校对所有文件的md5值
  7. 相机姿态估计(三)--P3P
  8. Solr4.8.0源码分析(13)之LuceneCore的索引修复
  9. java后端主要做什么_java后端开发需要学什么
  10. 统计年鉴29份3种格式混合
  11. 微信支付:手机系统自带的浏览器,调用微信支付如何实现(非扫码)
  12. 今年阿里巴巴重要开源项目全在这里
  13. 股票大作手操盘术---到手的利润
  14. IP地址0.0.0.0是什么意思?
  15. oracle根据关键字搜索存储过程
  16. windows正版系统下载地址
  17. QT课程设计:基于QT的图像处理程序
  18. python怎么升级python的pip
  19. 人脸验证与识别——从模型训练到项目部署
  20. SAP中国研究院总裁芮祥麟:从千里马到伯乐

热门文章

  1. MySQL数据库(五)
  2. 2020项目商机_营销“心”思维,赢得“新”商机 ——2020年第二期军师项目顺利落幕...
  3. 计算机网路网络层之IP协议(3)——IP编址
  4. 交换机 Telnet远程登录配置
  5. Ansible详解(二)——Ansible安装与命令
  6. NYOJ--975--关于521
  7. php烟花效果,用p5.js制作烟花特效的示例代码
  8. maven 不能设置为web2.5的解决方法
  9. python整数浮点数复数类型判断函数_Python数值类型(整形、浮点型和复数)及其用法讲解...
  10. 计算机在热处理上有何应用,计算机在热处理中的应用