如何以编程方式测试asp.net位置文件夹授权 [英] How to test asp.net location folder authorization programmatically

查看:85
本文介绍了如何以编程方式测试asp.net位置文件夹授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的web.config中有一个location元素,如下所示:

I have an location element in my web.config like so:

<location path="Admin">
    <system.web>
        <authorization>
            <allow roles="Domain\Development"/>
            <deny users="*" />
        </authorization>
    </system.web>
</location>

这仅适用于开发组成员对此文件夹的访问.

This works to only allow members of the development group access to this folder.

我想知道是否有一种方法可以简单地测试用户是否有权访问此文件夹?

I was wondering if there is a way to simply test if a user has access to this folder?

一种情况是创建菜单项.如果用户没有适当的权限,我只想隐藏或不呈现此文件夹中页面的链接.

One scenario is creating menu items. I'd simply like to hide or not render links to pages in this folder if the user does not have the proper rights.

有没有办法在代码中做到这一点.我不想对Domain \ Development中的成员资格进行硬编码检查,而是希望使用asp.net告诉我此当前用户是否具有访问权限.

Is there a way to do this in code. I don't want to have to hard code a check for membership in Domain\Development rather I'd like to use asp.net to tell me if this current user has access.

如果规则变得更复杂等,这将是很好的.将其放在一个位置会强制执行DRY(不要重复自己).

This would be nice if the rules get more complicated etc. Also having this in one place enforces DRY (Don't Repeat Yourself).

推荐答案

通过此处.)您可以为其指定一个路径和一个用户(例如,当前用户为Page.User),它会告诉您该用户是否可以访问该路径.

Through a related question I found the answer. Call UrlAuthorizationModule.CheckUrlAccessForPrincipal. (Documentation is here.) You can give it a path and a user (such as Page.User for the current user), and it will tell you if the user can access that path.

完全出于您提到的原因,我喜欢这一点:您可以将访问逻辑放在一个地方.

I like this for exactly the reason you mentioned: You can put your access logic in one place.

这篇关于如何以编程方式测试asp.net位置文件夹授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆