2009-08-06 81 views
3

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

我似乎无法找出这一个。

回答

8

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

比方说,您已经定义和授权部分为:

<system.web> 
<authorization> 
    <allow roles="admin,moderator" /> 
    <deny users="?" /> 
</authorization></system.web> 

您刚刚创建的部分意味着谁拥有管理和/或主持人角色可以访问内的网页,并拒绝大家用户(匿名)谁试图访问没有登录信息。

为了刚刚叫出WebConfigurationManager的GetSection方法

AuthorizationSection auth = WebConfigurationManager.GetSection("system.web/authorization") as AuthorizationSection; 

AuthorizationSection教室将给你 Rules收集这恰恰是你在找什么。

+0

我没有在'AuthenticationSection'对象上找到任何'Rules'集合...? – awe 2009-11-01 11:29:13

+0

AuthorizationSection包含规则集合。 我拼错了类的名字。这不是AuthenticationSection,而是AuthorizationSection,我编辑了我的帖子来验证你提到的错误,谢谢你注意到我。 – Myra 2009-11-10 13:05:21