• λ我爱Aspx >> Asp.Net >> ASP.NET 2.0中保证应用程序的安全
  • ASP.NET 2.0中保证应用程序的安全

  • :陶刚编译  Դ:天极开发  :2007-3-18 16:42:36  ؼ:.net,asp.net,asp
  • 用角色管理器对页面进行授权访问

      这个例子的web.config文件包含了<authorization>元素,它限制了示例只能让"Administrators"角色的成员访问。请确保你已经建立了"Administrators"角色并把自己添加到了这个角色中。一旦你称为"Administrators"角色的成员,就可以访问示例页面了。ASP.NET提供了一个角色管理器HttpModule,它自动地把RolePrincipal附加到当前请求的HttpContext上。如果你是"Administrators"角色的成员,当URL授权过程根据RolePrincipal执行IsInRole检查(URL授权过程调用RolePrincipal.IsInRole)的时候,该访问检查会返回true,你就可以访问页面了。请注意,你可以通过调用Page.User并把结果转换RolePrincipal来引用页面中的RolePrincipal。

    <location path="administrators_role">
    <system.web>
    <authorization>
    <allow roles="Administrators" />
    <deny users="*"/>
    </authorization>
    </system.web>
    </location>

      编程检查授权

      由于角色管理器特性把RolePrincipal附加到HttpContext上,你也可以编写代码根据RolePrincipal执行访问检查。你先建立两个角色"Regular Users"和"Power Users",把自己添加到这两个角色中。当你运行示例的时候,页面使用多种技术执行IsInRole检查。有些访问检查使用了User.IsInRole。它说明了使用正常的Page.User语法的时候,RolePrincipal也是可用的。这个页面还演示了如何把Page.User转换为RolePrincipal引用,接着直接在RolePrincipal上调用IsInRole。

    <asp:Label ID="Label1" runat="server" Text=<%# User.IsInRole("Administrators") %> />
    <asp:Label ID="Label2" runat="server" Text=<%# Roles.IsUserInRole("Regular Users") %> />
    <asp:Label ID="Label3" runat="server" Text=<%# (CType(User,RolePrincipal)).IsInRole("Power Users") %> />
    Ҷƪл˵?
  • һƪASP.NET动态生成html页面
    һƪASP.NET编程入门随想之宽容