访问web.config中的授权信息

kar*_*eem 4 .net authorization roleprovider

我正在编写自定义角色提供程序,我需要以编程方式访问存储在web.config中的授权信息.网站的某些部分只能由某些角色访问.我想知道哪些角色可以访问某个页面和/或某个角色可以访问哪个页面.

我似乎无法想出这个.

Myr*_*yra 9

您可以通过System.Web.Security命名空间中的WebConfigurationManager类访问存储的任何信息,如ConnectionStrings,AppSettings和web.config中的其他已定义值.

假设您已定义和授权部分为:

<system.web>
<authorization>
  <allow roles="admin,moderator" />
  <deny users="?" />
</authorization></system.web>
Run Code Online (Sandbox Code Playgroud)

您刚刚创建的部分意味着具有管理员和/或管理员角色的用户可以访问其中的页面,并拒绝在没有登录信息的情况下尝试访问的所有人(匿名).

为此,只需将WebConfigurationManager的GetSection方法调用为

AuthorizationSection auth = WebConfigurationManager.GetSection("system.web/authorization") as AuthorizationSection;
Run Code Online (Sandbox Code Playgroud)

AuthorizationSection类将为您提供 Rules正是您正在寻找的集合.