简体   繁体   English

使用Web.config位置元素替代Roles.IsUserInRole(“ rolename”)

[英]Alternative to Roles.IsUserInRole(“rolename”) using Web.config location element

Let me start by saying that what I have works - I just don't like it. 首先,我要说我的作品有用-我只是不喜欢它。

I have a web.sitemap that is used for my navigation bar. 我有一个用于导航栏的web.sitemap。 Some of the nodes are accessible by everyone, some of them have roles assigned (so that the node won't be shown to users who wouldn't be able to access it). 每个人都可以访问某些节点,其中一些节点分配有角色(这样,该节点将不会显示给无法访问该节点的用户)。

ie <siteMapNode url="~/adminpage.aspx" title="admin" roles="domain\\Administrators"/> <siteMapNode url="~/adminpage.aspx" title="admin" roles="domain\\Administrators"/>

Then, in my web.config, I have location elements like this: 然后,在我的web.config中,我具有如下位置元素:

<location path="adminpage.aspx">
  <system.web>
    <authorization>
      <allow roles="domain\Administrators"/>
      <deny roles="*"/>
    </authorization>
  </system.web>
</location>

In addition to this, some pages have hyperlinks that are shown or hidden based on roles, like this: 除此之外,某些页面还具有根据角色显示或隐藏的超链接,例如:

myHyperlink.Visible = Roles.IsUserInRole(@"domain\Administrators");

All of this works just fine - but... it seems a bit ridiculous. 所有这些工作都很好-但是...似乎有点荒谬。 Is there any way to utilize the permissions established in my Web.config by the sitemap and Roles.IsUserInRole? 有什么方法可以利用站点地图和Roles.IsUserInRole在Web.config中建立的权限吗? Basically, I'm trying to prevent having the same permission descriptions in 435234 different places. 基本上,我试图防止在435234个不同位置具有相同的权限描述。

I attempted Casses solution to add embedded code in the sitemap file that would use settings from the web config - but that doesn't work (because you can't put embedded code in the sitemap file). 我尝试了Casses解决方案在站点地图文件中添加嵌入式代码,该代码将使用Web配置中的设置-但这不起作用(因为您无法将嵌入式代码放入站点地图文件中)。

Your examples don't actually define the same behaviour in 3 different ways. 您的示例实际上并未以3种不同的方式定义相同的行为。 The first and third are similar, though done differently. 第一个和第三个相似,但是做得不同。 They prevent a user from seeing the links. 它们阻止用户看到链接。 But, if the user knows the URL to go directly to the target, they can bypass the links and get there anyway. 但是,如果用户知道将URL直接转到目标,则可以绕过链接并以任何方式到达目标。

That's where the second example you posted comes in. It prevents loading the page unless you're in the correct role. 这就是您发布的第二个示例的位置。除非您担任正确的角色,否则它会阻止加载页面。

If you don't care about showing the links to everywhere, the second example is all you need. 如果您不希望显示到任何地方的链接,那么第二个示例就是您所需要的。 Users will be able to see the element on the sitemap and the links on whatever pages they're on, but clicking them will not bring them to the admin page. 用户将能够在其所处的任何页面上看到站点地图上的元素和链接,但是单击它们不会将其带到管理页面。 Whether or not that's acceptable for your project is up to you. 是否可以接受您的项目取决于您。

If you want to hide the links and limit the amount of code you right to do so, I would recommend something like this: 如果您想隐藏链接并限制您执行此操作的代码量,我建议您这样做:

<% if Roles.IsUserInRole(@"domain\Administrators") { %>
<a href="~/adminpage.aspx">Admin Page</a>
... any other links you want to limit to the admin role ...
<% } %>

That will at least keep the code to a minimum. 这样至少可以使代码最少。 The links won't even be added to the DOM unless the authenticated user is in that role, and if at a later date you need to change what role is being checked, you have to change it in fewer locations. 除非经过身份验证的用户担任该角色,否则这些链接甚至都不会添加到DOM,并且如果以后需要更改要检查的角色,则必须在较少的位置进行更改。

Either way, the web.config entry you gave should be included. 无论哪种方式,都应包含您提供的web.config条目。 You shouldn't assume that a malicious user doesn't know what your admin page is called. 您不应该假定恶意用户不知道您的管理页面被称为什么。 Especially when it's called adminpage. 特别是当它被称为管理页面时。

EDIT 编辑

To reduce the duplication of the role name, try defining it in a section of the web.config and referencing it from there. 为了减少角色名称的重复,请尝试在web.config的一部分中定义它,然后从那里引用它。 You won't be able to do that for the locations tag, but you should be able to everywhere else. 您将无法对locations标签执行此操作,但是您应该可以在其他任何地方执行此操作。

ConfigurationManager.AppSettings["AdminRole"]

for example. 例如。

In the Sitemap, try 在站点地图中,尝试

<siteMapNode url="~/adminpage.aspx" title="admin" roles="<% ConfigurationManager.AppSettings["AdminRole"]%>"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM