打造坚固的企业应用.NET 安全基础

[ 3298 查看 / 0 回复 ]

网上看到,转帖分享d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  托管代码的优点d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  开发 .NET Framework 应用程序提供了一些直接的安全性优点,但是还有许多问题需要考虑。这些问题在本指南第三部分的几个“构建……”单元中进行了讨论。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  .NET Framework 程序集是用托管代码生成的。各种语言的编译器(例如 Microsoft Visual C#_ 开发工具和 Microsoft Visual Basic疇.NET 开发系统)都输出 Microsoft 中间语言 (MSIL) 指令,它们包含在标准 Microsoft Windows 可移植的执行文件 (PE) .dll 或 .exe 文件中。当加载程序集并且调用一个方法时,方法的 MSIL 代码将被实时 (JIT) 编译器编译为本机的机器指令,随后再执行这些机器指令。不会调用到的方法将不进行 JIT 编译。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  中间语言与公共语言运行库所提供的运行库环境配合使用,为程序集开发人员提供了许多直接的安全性优点。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  文件格式与元数据验证。 公共语言运行库能够验证 PE 文件格式的有效性,以及地址不指向 PE 文件之外。这有助于提供程序集独立。公共语言运行库还能验证程序集中所包含的元数据的完整性。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  代码验证。MSIL 代码在进行 JIT 编译时会验证类型安全性。从安全角度来看,这是一个很大的优点,因为验证过程可以防止不良的指针操作,可以验证类型转换的合法性,检查数组边界,等等。这实际上消除了托管代码中出现缓冲溢出漏洞的可能,虽然仍旧需要仔细检查调用非托管应用程序编程接口 (API) 的代码是否存在缓冲溢出的可能。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  完整性检查。强名称程序集的完整性需要使用数字签名来验证,以确保程序集从生成和签名之后没有发生任何改变。这意味着攻击者无法通过直接操作 MSIL 指令来改变代码。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  代码访问安全性。公共语言运行库提供的虚拟执行环境还允许在运行时执行更多安全检查。说得具体一些,就是代码访问安全性能够根据调用代码的标识进行各种运行时安全决策。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  用户安全性与代码安全性d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  用户安全性和代码安全性是 .NET Framework 应用程序中两种相辅相成的安全形式。用户安全性回答的是“用户是谁,用户能进行什么操作?”,而代码安全性回答的是这样的问题:“代码从何而来,谁编写了代码,代码能执行什么操作?” 代码安全性涉及的是授权对应用程序(而非用户)系统级资源(包括文件系统、注册表、网络、目录服务和数据库)的访问。在此情况下,无论最终用户是谁,或者哪个用户帐户运行代码都是无关紧要的,而什么代码以及是否允许进行这样的操作则非常重要。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  .NET Framework 用户安全性的实现称为基于角色的安全性。 代码安全性的实现称为代码访问安全性。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  基于角色的安全性d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  .NET Framework 基于角色的安全性允许 Web 应用程序根据与应用程序交互的用户的标识或者角色成员身份进行安全决策。如果应用程序使用 Windows 身份验证,则角色就是 Windows 组。如果应用程序使用其他形式的身份验证,则角色就是由应用程序定义的,用户和角色的详细信息通常维持在 SQL Server 或者基于 Active Directory 的用户存储中。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  已经进行身份验证的用户的身份及其相关的角色成员身份是 Web 应用程序通过 Principal 对象获得的,后者附加在当前 Web 请求中。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  图 1 显示了 Web 应用程序中如何使用用户安全性来限制用户访问 Web 页、业务逻辑、操作和数据访问的典型逻辑视图。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ

 附件: 您所在的用户组无法下载或查看附件d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
图 1. 基于(用户)角色的安全性的逻辑视图d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  代码访问安全d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  代码访问安全可以在代码试图访问安全的资源(例如,文件系统、注册表、网络等等)时,或者试图执行其他特权操作(例如,调用非托管代码或者使用反射)时,对其进行授权。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  代码访问安全性是一种重要的附加防范机制,可以用来提供对一段代码的约束。管理员可以配置代码访问安全策略以限制代码能够访问的资源类型以及能够执行的其他特权操作。从 Web 应用程序的角度来看,这意味着如果在某个危及安全的攻击过程中,攻击者控制了 Web 应用程序进程或者插入了在进程中运行的代码,则代码访问安全性提供的这些附加约束就能够限制可能带来的损害。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  图 2 显示了在 Web 应用程序中如何使用代码访问安全性来限制应用程序访问系统资源、其他应用程序拥有的资源和特权操作(如调用非托管代码)的逻辑视图。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ

 附件: 您所在的用户组无法下载或查看附件d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
图 2. 基于代码的安全性的逻辑视图d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  代码的身份验证(标识)是根据代码的证据(例如,它的强名称、发行者,或者安装目录)进行的。而授权是根据安全策略授予代码的代码访问权限进行的。有关 .NET Framework 代码访问安全性的详细信息,请参阅“代码访问安全性实践”单元。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  .NET Framework 基于角色的安全性d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  .NET Framework 基于角色的安全性是在应用程序中用来授权用户操作的关键技术。角色经常用来强制执行一些业务规则。例如,财务应用程序可能只允许经理执行超过某个特定金额上限的转账操作。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  基于角色的安全性由以下要素组成:d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  用户和标识d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  PrincipalPermission 对象d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  基于角色的安全性检查d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  URL授权d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  用户和标识d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  基于角色的安全性是通过 Principal 和 Identity 对象实现的。已进行身份验证的调用方的标识和角色成员身份是通过 Principal 对象公开的,该对象附加在当前 Web 请求中。可以使用 HttpContext.Current.User 属性检索该对象。如果应用程序不需要对调用方进行身份验证,例如,用户正在浏览站点中可以公开访问的部分,则 Principal 对象代表的是匿名 Internet 用户。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  Principal 对象有很多类型,准确的类型取决于应用程序所使用的身份验证机制。但是,所有 Principal 对象都要实现 System.Security.Principal.IPrincipal 接口,它们都维持着一个角色列表,用户是其中的一个成员。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  Principal 对象还包含 Identity 对象,后者包括用户名以及指示身份验证类型和用户是否已经进行身份验证的标志。这样就能够区分已进行身份验证和匿名的用户。Identity 对象也有许多不同类型,取决于身份验证的类型,虽然所有标识对象都要实现 System.Security.Principal.IIdentity 接口。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  以下表格给出了可能的身份验证类型以及 ASP.NET Web 应用程序使用的 Principal 和 Identity 对象的各种不同类型。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ

 附件: 您所在的用户组无法下载或查看附件d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  PrincipalPermission 对象d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  PrincipalPermission 对象表示当前用户执行代码所必需的标识和角色。PrincipalPermission 对象可以在代码中声明性或命令性地使用。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  声明性安全d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  可以通过在类或者方法定义中添加 PrincipalPermissionAttribute,准确地控制允许哪些用户访问这些类或者方法。类级别的属性自动应用于所有类成员,除非它已被成员级属性所重写。PrincipalPermissionAttribute 类型是在 System.Security.Permissions 命名空间中定义的。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  注 还可以使用 PrincipalPermissionAttribute 限制对结构和其他成员类型(如属性和委托)的访问。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  下面的示例说明了如何将对某个特定的类的访问权限制在 Managers 组的成员中。请注意该示例假设使用 Windows 身份验证,其中角色名的格式是 MachineName\RoleName 或者 DomainName\RoleName。对于其他的身份验证类型,角色名的格式是特定于应用程序的,并取决于用户存储中保存的角色名字符串。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
   [PrincipalPermissionAttribute(SecurityAction.Demand, Role=@"DOMAINNAME\Managers")]d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  public sealed class OnlyManagersCanCallMed*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  {d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  }d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  注 属性类型名中最后的 Attribute 可以省略。这样属性类型名就与其相关的权限类型名相同了,在此例中为 PrincipalPermission。它们是截然不同的(但逻辑上是相关的)类型。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  下面的示例说明了如何限制对一个类的特定方法的访问。在此示例中,访问仅限于本地管理员组的成员,这是通过特殊的 "BUILTIN\Administrators" 标识符来标识的。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
   [PrincipalPermissionAttribute(SecurityAction.Demand,Role=@"BUILTIN\Administrators")]d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  public void SomeMethod()d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  {d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  }d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  其他内置的 Windows 组名可以通过在组名前加上"BUILTIN\"(例如,"BUILTIN\Users" 和 "BUILTIN\Power Users" )使用。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
   命令性安全d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  如果方法级安全性对于您的安全需求来说还不够细致,可以在代码中执行命令性安全检查,方法是使用 System.Security.Permissions.PrincipalPermission 对象。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  以下示例说明了使用 PrincipalPermission 对象的命令性安全语法。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  PrincipalPermission permCheck = new PrincipalPermission(null, @"DomainName\WindowsGroup");d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  permCheck.Demand();d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  为了避免使用局部变量,以上代码还可以写成:d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
   (new PrincipalPermission(null, @"DomainName\WindowsGroup")).Demand();d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  以上代码用一个空的用户名和指定的角色名创建了一个 PrincipalPermission 对象,然后调用了 Demand 方法。这将导致公共语言运行库查询附加于当前线程的当前 Principal 对象,检查相关联的标识是否为指定角色的成员。因为本例中使用了 Windows 身份验证,所以角色的检查使用的是 Windows 组。如果当前标识不是指定角色的成员,将发生 SecurityException 异常。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  以下示例说明了如何限制对某个单独用户的访问。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
   (new PrincipalPermission(@"DOMAINNAME\James", null)).Demand();d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  声明性安全与命令性安全d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  基于角色的安全(和代码访问安全)既可以通过属性声明性地使用,也可以在代码中命令性地使用。一般而言,声明性安全能够提供最多好处,但是有时候必须使用命令性安全(例如,需要使用只能在运行时访问的变量时)帮助进行安全性决策。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  声明性安全的优点d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  声明性安全的主要优点如下:d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  它允许管理员或者程序集的使用者准确地看到特定的类和方法必须以何种安全权限运行。诸如 permview.exe 这样的工具可以提供此类信息。在部署时了解这些信息可以帮助解决安全性问题,还能帮助管理员配置代码访问安全性策略。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  它提供了更好的性能。声明性请求只能在加载时计算一次。方法中的命令性请求则在每次调用包含此请求的方法时都要计算。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  安全属性确保了权限请求在方法中其他任何代码可能运行之前执行。这消除了因为安全检查执行得太迟带来的潜在错误。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  类级的声明性检查应用于所有类成员。命令性检查则在调用处应用。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
   命令性安全的优点d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  命令性安全的主要优点以及有时候必须使用命令性安全的主要原因有:d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  它允许您使用只能在运行时获得的值动态地形成请求。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  它允许您通过在代码中实现条件逻辑,执行更细致的授权。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  基于角色的安全性检查d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  对于较细的授权决策,还可以执行显式的角色检查,方法是使用 IPrincipal.IsInRole 方法。以下示例假设使用 Windows 身份验证,虽然代码与使用窗体身份验证时非常相似,只不过需要将 User 对象强制转换为 GenericPrincipal 类型的对象。
  1. // Extract the authenticated user from the current HTTP context.
  2. d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  3.   // The User variable is equivalent to HttpContext.Current.User if you are using
  4. d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  5.   // an .aspx or .asmx page
  6. d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  7.   WindowsPrincipal authenticatedUser = User as WindowsPrincipal;
  8. d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  9.   if (null != authenticatedUser)
  10. d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  11.   {
  12. d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  13.    // Note: If you need to authorize specific users based on their identity
  14. d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  15.    // and not their role membership, you can retrieve the authenticated user's
  16. d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  17.    // username with the following line of code (normally though, you should
  18. d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  19.    // perform role-based authorization).
  20. d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  21.    // string username = authenticatedUser.Identity.Name;
  22. d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  23.   
  24. d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  25.    // Perform a role check
  26. d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  27.    if (authenticatedUser.IsInRole(@"DomainName\Manager") )
  28. d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  29.    {
  30. d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  31.     // User is authorized to perform manager functionality
  32. d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  33.    }
  34. d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  35.   }
  36. d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  37.   else
  38. d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  39.   {
  40. d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  41.    // User is not authorized to perform manager functionality
  42. d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  43.    // Throw a security exception
  44. d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  45.   }
  46. d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
复制代码
URL 授权d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  管理员可以通过使用 Machine.config 或 Web.config 中的 <authorization >元素配置基于角色的安全性。该元素配置 ASP.NET UrlAuthorizationModule,它使用附加于当前 Web 请求中的 Principal 对象,以做出授权决策。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  授权元素还包含子元素 <allow >和 <deny>,这些子元素用来确定允许或者不允许哪些用户或者组访问特定的目录和页面。除非 <location> 元素中包含 <authorization> 元素,否则 Web.config 中的 <authorization> 元素将控制对 Web.config 文件所在目录的访问。该目录通常是 Web 应用程序的虚拟根目录。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  Web.config 的以下示例将使用 Windows 身份验证,允许 Bob 和 Mary 访问,但是拒绝其他所有人:d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  <authorization>d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  <allow users="DomainName\Bob, DomainName\Mary" />d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  <deny users="*" />d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  </authorization>d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  以下语法和语义适用于 <authorization >元素的配置:d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  "*" 指代所有标识。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  "?" 指代未进行身份验证的标识(即匿名标识)。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  使 URL 授权起作用不需要进行模拟。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  URL 授权的用户和角色是由身份验证设置决定的:d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  当有 <authentication mode="Windows" />时,将授权可以对 Windows 用户和组帐户进行访问。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  用户名的形式是“DomainName\WindowsUserName”。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  角色名的形式是“DomainName\WindowsGroupName”。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  注 本地管理员组称为“BUILTIN\Administrators”。本地用户组称为“BUILTIN\Users”。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  当有 <authentication mode="Forms" />时,存储在当前 HTTP 上下文中的 IPrincipal 对象的用户和角色将得到授权。例如,如果使用窗体在数据库中对用户进行身份验证,则从数据库中检索到的角色将得到授权。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  当有 <authentication mode="Passport" />时,从存储中检索到的 Passport 用户 ID (PUID) 或者角色将获得授权。例如,可以将一个 PUID 映射到存储在 Microsoft SQL Server 数据库或 Active Directory 中特定的帐户和角色集。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  当有 <authentication mode="None" />时,可能就无需进行授权了。“None”指定了不需要执行任何身份验证,或者不需要使用任何 ASP.NET 身份验证模块,而是使用自己的自定义机制。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  但是,如果使用自定义身份验证,应该创建一个包含多个角色的 IPrincipal 对象,并将其存储在 HttpContext.Current.User 属性中。在以后执行 URL 授权时,将对 IPrincipal 对象中维持的用户和角色(无论它们如何检索而来)进行授权。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  配置对特定文件的访问d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  要配置对特定文件的访问,请将 <authorization> 元素放在 <location>元素内,如下所示。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  <location path="somepage.aspx" />d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  <authorization>d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  <allow users="DomainName\Bob, DomainName\Mary" />d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  <deny users="*" />d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  </authorization>d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  </location>d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  还可以将 path 属性指向一个特定文件夹,将访问控制应用到该特定文件夹中的所有文件。有关 <location> 元素的更多信息,请参阅“保护 ASP.NET 应用程序安全性”单元。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  .NET Framework 安全性命名空间d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  对 .NET Framework 安全性进行编程,要使用 .NET Framework 安全性命名空间中的类型。本节介绍了开发安全的 Web 应用程序时可能用到的这些命名空间和类型。有关类型的完整列表,请参阅 .NET Framework 文档。安全性命名空间如下,同时显示在图 3 中。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  System.Securityd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  System.Web.Securityd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  System.Security.Cryptographyd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  System.Security.Principald*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  System.Security.Policyd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  System.Security.Permissionsd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ

 附件: 您所在的用户组无法下载或查看附件d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
图" 3. .NET Framework 安全性命名空间d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  System.Securityd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  这个命名空间包含 CodeAccessPermission 基类,所有其他的代码访问权限类型都由此派生。直接使用基类是不可能的。更可能的情形是:使用特定权限类型表示代码访问特定资源类型或执行其他特权操作的权限。例如,FileIOPermission 表示执行文件 I/O 的权限,EventLogPermission 表示代码访问事件日志的权限,等等。有关代码访问权限类型的完整列表,请参阅本单元后面的表 2。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  System.Security 命名空间还包含封装了权限集的类。这包括 PermissionSet 和 NamedPermissionSet 类。在构建安全的 Web 应用程序时最可能使用的类型是:d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  SecurityException 。用来表示安全性错误的异常类型。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  AllowPartiallyTrustedCallersAttribute 。这是一个程序集级属性,用于必须支持部分受信任调用方的那些具有强名称的程序集。如果不使用这个属性,具有强名称的程序集只能被完全受信任的调用方(有无限权限的调用方)调用。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  SupressUnmanagedSecurityAttribute 。用来优化性能,并使平台调用服务 (P/Invoke) 和组件对象模型 (COM) 互操作层不再需要授予非托管代码权限。这个属性必须小心使用,因为它暴露了一个潜在的安全风险。如果攻击者获得了对非托管代码的控制,他就不再受代码访问安全性的限制了。有关如何安全使用这一属性的更多信息,请参阅单元“代码访问安全性实践”中“非托管代码”一节。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  System.Web.Securityd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  这个命名空间包含用于管理 Web 应用程序身份验证和授权的类。这包括 Windows、窗体和 Passport 身份验证、URL 和文件授权,这些分别是由 UrlAuthorizationModule 和 FileAuthorizationModule 类控制的。在构建安全的 Web 应用程序时最可能使用的类型是:d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  FormsAuthentication 。提供静态方法辅助窗体身份验证和身份验证票的操作。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  FormsIdentity 。用于封装已经过窗体身份验证的用户标识。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  PassportIdentity。用于封装已经过 Passport 身份验证的用户标识。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  System.Security.Cryptographyd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  这个命名空间包含了用来执行加密和解密、哈希和随机数生成的类型。这是一个很大的命名空间,包含许多类型。许多加密算法都是用托管代码实现的,而其他一些加密算法则通过此命名空间中的类型公开,包装了基于 Microsoft Win32? 的 CryptoAPI 所提供的基础加密功能。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  System.Security.Principald*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  这个命名空间包含了用于支持基于角色的安全性的类型。它们用来限制哪些用户能够访问类和类成员。此命名空间包括 IPrincipal 和 IIdentity 接口。在构建安全的 Web 应用程序时最可能使用的类型是:d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  GenericPrincipal 和 GenericIdentity。可以用来定义自己的角色和用户标识。它们通常都在自定义身份验证机制中使用。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  WindowsPrincipal 和 WindowsIdentity 。表示一个已经用 Windows 身份验证与用户相关联的 Windows 组(角色)列表进行身份验证的用户。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  System.Security.Policyd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  这个命名空间包含用于实现代码访问安全性策略系统的类型。它包括表示代码组、成员条件、策略级别和证据的类型。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  System.Security.Permissionsd*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  这个命名空间包含用来封装代码访问资源、执行特权操作的权限的主要权限类型。下表列出了这个命名空间中定义的权限类型(以字母顺序排列)。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ

 附件: 您所在的用户组无法下载或查看附件d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  SecurityPermission" 类值得特别注意,因为它表示代码执行特权操作的权限,包括断言代码访问权限、调用非托管代码、使用反射、控制策略和证据等等。 SecurityPermission 类确定的准确权限是由其 Flags 属性确定的,后者必须设置为 SecurityPermissionFlags 枚举类型(例如,SecurityPermissionFlags.UnmanagedCode )定义的枚举值之一。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  小结d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  本单元通过比较用户安全性和代码安全性,以及探讨安全性命名空间,向您介绍了 .NET Framework 安全性的全貌。.NET Framework 分别将这两种安全性类型称为基于角色的安全性和代码访问安全性。两种形式的安全性都位于 Windows 安全性的顶层。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
  基于角色的安全性关心的是对用户访问应用程序管理的资源(例如 Web 页)和操作(例如业务和数据访问逻辑)进行访问。代码访问安全性关心的是约束特权代码,准确控制哪些代码能够访问资源并执行其他特权操作。对于 Web 应用程序而言,这是一种强大的附加安全性机制,因为它限制了攻击者的所作所为,即使攻击者会想方设法对 Web 应用程序进程产生威胁。对于提供应用程序独立性而言,这也是一种极为强大的功能。对于宿主公司或者任何在同一个 Web 服务器上宿主多个 Web 应用程序的单位来说,尤其如此。d*µQÀeÈ©C¨nfhot.com.–xŸÆilGÞ
本主题由 管理员 jerry 于 2009-5-17 19:01:20 执行 设置精华/取消 操作
学而时习之,不亦说乎?有朋自远方来,不亦乐乎?人不知而不愠,不亦君子乎?
知之为知之,不知为不知,是知也。
TOP