Accessing authorization information in web.config

允我心安 提交于 2019-12-05 19:07:12

You can access any information stored such as ConnectionStrings,AppSettings and other defined values in web.config by WebConfigurationManager class in System.Web.Security namespace.

Let's say you have defined and authorization section as:

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

The section you just created means users who has admin and/or moderator roles can access pages within and deny everyone (anonymous) who attempts to access without login information.

In order to that just call out WebConfigurationManager's GetSection method as

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

AuthorizationSection class will give you Rules collection which is precisely what you're looking for.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!