如何以编程方式列出哪些 ASP.Net 角色可以访问页面?
有没有一种方法可以通过代码列出哪些角色可以访问给定页面?
例如,我有一个 Testpage.aspx,我想列出当用户访问该页面时该页面允许的角色。 URLAuthorizationManager 必须能够以某种方式找到这一点,因此必须有一种方法它知道在页面的 webconfig 中配置了哪些角色。或网址。
这是限制允许查看此页面的角色的 Web 配置。
<location path="Testpage.aspx">
<system.web>
<authorization>
<allow roles ="admin,sales" />
</authorization>
</system.web>
</location>
如果我能找到解决方案,它将返回“admin”、“sales”。有人知道我该怎么做吗?谢谢
Is there a way of listing which roles have access to a given page via code?
Example, I have a Testpage.aspx, and I wanted to list the roles allowed for this page when a user accesses the page. The URLAuthorizationManager must be able to find this out somehow, so there must be a way it knows what roles are configured in the webconfig for a page. or URL.
Here is the webconfig limiting the roles allowed to view this page.
<location path="Testpage.aspx">
<system.web>
<authorization>
<allow roles ="admin,sales" />
</authorization>
</system.web>
</location>
If I could find a solution, it would return "admin", "sales". Any one know how I can do this? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在想要获取信息的页面中使用以下代码。
调用
First()
的原因是 .NET 配置是分层的。假设您有以下网站层次结构和配置:并且您从
Test.aspx.cs
调用上面的代码,然后属性AuthorizationSection.Rules
包含分别对应的三个项来自/SubDir/Web.config
、Web.config
和machine.config
的配置。因此第一个元素包含角色admin
和other
。You can use the following code inside the page where you want to obtain the information.
The reason for the call to
First()
is that .NET configuration is hierarchical. Suppose you have the following web site hierarchy and configuration:and you call the code above from
Test.aspx.cs
, then the propertyAuthorizationSection.Rules
contains three items corresponding to respectively the configuration from/SubDir/Web.config
,Web.config
andmachine.config
. So the first element contains the rolesadmin
andother
.我的问题非常相似,只是我需要能够迭代所有的
目录和相关子目录,并显示每个网页和文件夹目录允许的角色。我无法使用 Ronald Wildenberg 的解决方案,因为我们使用的是 .Net 2.0,所以我们没有 Linq 功能。
他的解决方案为我提供了所需的路线图。我还从 Microsoft 的法国 IIS 支持团队找到了帮助,以编程方式管理表单身份验证。我不想像他们发布的那样重写配置文件,而是我们需要能够显示应用程序中所有目录和页面允许的角色。我们的应用程序很小。它总共有 15 个目录和不到 100 个页面,因此运行速度相当快。您的里程可能会根据您网站的大小而有所不同。
我从根目录开始递归搜索所有 webconfig。我将它们及其路径添加到字符串列表中,然后迭代该列表并调用我的 ListRoles 函数。此函数打开 Web 配置并获取位置集合。然后它会像 Ronald 一样查找“system.web/authorization”。如果找到授权部分,它将循环遍历规则并排除任何继承的规则,并重点关注具有关联角色的 AuthorizationRuleAction.Allow:
来自法国 IIS 支持团队博客
My problem was very similar except I needed the ability to iterate through all of the
directories and related subdirectories and display allowed roles for each web page and folder directory. I was unable to use Ronald Wildenberg's solution because we're using .Net 2.0 so we don't have the Linq functionality.
His solution gave me the roadmap I needed. I also found help from from Microsoft's French IIS Support Team, Managing Forms Authentication Programmatically. I didn't want to rewrite the config files like they posted, rather we needed the ability to show the allowed roles for all directories and pages in our application. Our application is small. It has a total of 15 directories and less than 100 pages so this runs pretty quickly. Your mileage my vary depending on the size of your web site.
I started from the root directory and recursively searched for all webconfigs. I added them with their path to a string list then iterated through the list and called my ListRoles function. This function opens the web config and gets the location collection. Then it looks for the "system.web/authorization" like Ronald did. If it finds an authorization section it loops through the rules and excludes any inherited rules and focuses on AuthorizationRuleAction.Allow with associated roles:
From French IIS support Team blog
使用 Roles.GetAllRoles() 方法
http: //msdn.microsoft.com/en-us/library/system.web.security.roles.getallroles.aspx
下面是一个示例,其中列出了所有角色:
http://weblogs.asp.net/scottgu/archive/ 2005/10/18/427754.aspx
Use the Roles.GetAllRoles() method
http://msdn.microsoft.com/en-us/library/system.web.security.roles.getallroles.aspx
and here is an example where they list all roles:
http://weblogs.asp.net/scottgu/archive/2005/10/18/427754.aspx