如何在 ASP.NET 中检查请求的路径 (URL) 是否具有身份验证规则?
我的 ASP.NET 解决方案中有一个文件夹层次结构,如下所示:
Reseller< 中的所有内容/code> 文件夹应该经过身份验证,并被视为安全资源。但
Services
文件夹中的所有内容都是公开的,无需验证针对 Web 服务 ProductServices.asmx
发出的任何请求。
现在,我想挂接到请求处理管道的 AuthenticateRequest 中,在对用户进行身份验证之前,我想查看该请求是针对公共路径还是安全路径。我知道我可以使用 UrlAuthorizationModule.CheckUrlAccessForPrincipal
并且我实际上已经在 另一个问题。但是 UrlAuthorizationModule.CheckUrlAccessForPrincipal
是一种可以在请求经过身份验证后使用的方法。但是,在进行任何身份验证之前,我想知道请求的路径是否安全。换句话说,是否在任何 web.config 文件的文件夹层次结构中的任何位置为所请求的路径定义了任何 authentication
元素。
我想要的伪代码可能是这样的:
UrlAuthorizationModule.IsRequestedPathSecure(Request.Url.AbsolutePath)
我该怎么做?
I have a folder hierarchy in my ASP.NET solution, like this:
Everything in Reseller
folder should be authenticated, and is considered a secure resource. But anything in Services
folder is just public, and there is no need to authenticate any request coming for the web service ProductServices.asmx
.
Now, I want to hook into the AuthenticateRequest
of the request process pipeline and there, before user is authenticated, I want to see if the request is for a public, or a secure path. I know that I can use UrlAuthorizationModule.CheckUrlAccessForPrincipal
and I actually have asked that in another question. But UrlAuthorizationModule.CheckUrlAccessForPrincipal
is a method which can be used, just after the request is authenticated. However, before any authentication, I want to know if the requested path is secure or not. In other words, is there any authentication
element defined for the requested path anywhere in it's folder hierarchy in any web.config file, or not.
A pseudo-code of what I want could be something like:
UrlAuthorizationModule.IsRequestedPathSecure(Request.Url.AbsolutePath)
How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
CheckUrlAccessForPrincipal
方法(如您所提到的),但使用表示匿名用户的GenericPrincipal
,如下所示:You could use the
CheckUrlAccessForPrincipal
method (as you mentioned) but using aGenericPrincipal
representing an anonymous user like so:不确定这是否有帮助,但您可以使用 web.config 的 location 元素来禁止/授予对隐藏资源的访问权限,请参阅 如何:控制 ASP.NET 应用程序中的授权权限了解说明。它使您可以根据文件夹或 aspx/asmx 授予访问权限。如果用户没有权限,IIS 将为禁止位置返回 403 HTTP 错误代码,并且不会处理对这些位置的请求
Not sure if this helps, but you can forbid/grant access to your hidden resources by using location element of web.config see HOW TO: Control Authorization Permissions in an ASP.NET Application for description. It gives you possibility of granting access on folder or aspx/asmx basis. IIS will return 403 HTTP error code for forbidden locations and not process requests to those if users don't have permissions
将 web.config 文件添加到 Reseller 并写入以下代码
,并将 web.config 文件添加到 Service 文件夹并写入以下代码
请注意页面主题=“”这是必要的。
Add a web.config file to Reseller and Write following Code into it
and also add a web.config file to Service folder and write follwoing code into it
Note to Page theme="" it is necessary.