如何在 ASP.NET 中检查请求的路径 (URL) 是否具有身份验证规则?

发布于 2024-12-08 15:27:42 字数 904 浏览 3 评论 0原文

我的 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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空气里的味道 2024-12-15 15:27:42

您可以使用 CheckUrlAccessForPrincipal 方法(如您所提到的),但使用表示匿名用户的 GenericPrincipal ,如下所示:

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
    IIdentity identity = new GenericIdentity(string.Empty, string.Empty);
    IPrincipal principal = new GenericPrincipal(identity, new string[] { });

    bool hasAccess = UrlAuthorizationModule.CheckUrlAccessForPrincipal(Request.Path, principal, "GET");

    if(!hasAccess)
    {
        //Anonymous access not permitted to the current URL.
    }
}

You could use the CheckUrlAccessForPrincipal method (as you mentioned) but using a GenericPrincipal representing an anonymous user like so:

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
    IIdentity identity = new GenericIdentity(string.Empty, string.Empty);
    IPrincipal principal = new GenericPrincipal(identity, new string[] { });

    bool hasAccess = UrlAuthorizationModule.CheckUrlAccessForPrincipal(Request.Path, principal, "GET");

    if(!hasAccess)
    {
        //Anonymous access not permitted to the current URL.
    }
}
月下凄凉 2024-12-15 15:27:42

不确定这是否有帮助,但您可以使用 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

鲜肉鲜肉永远不皱 2024-12-15 15:27:42

web.config 文件添加到 Reseller 并写入以下代码

<?xml version="1.0"?>
    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
      <system.web>
        <authorization>
          <allow roles="ResellerUser,ResellerAdmin" />
          <deny users="*"/>
        </authorization>
  </system.web>
</configuration>

,并将 web.config 文件添加到 Service 文件夹并写入以下代码

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <appSettings>
  </appSettings>
      <system.web>
        <pages theme="">
        </pages>
 <authorization>
  <allow roles="ResellerUser, ResellerAdmin" />
  <deny users="*" />
</authorization>

请注意页面主题=“”这是必要的。

Add a web.config file to Reseller and Write following Code into it

<?xml version="1.0"?>
    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
      <system.web>
        <authorization>
          <allow roles="ResellerUser,ResellerAdmin" />
          <deny users="*"/>
        </authorization>
  </system.web>
</configuration>

and also add a web.config file to Service folder and write follwoing code into it

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <appSettings>
  </appSettings>
      <system.web>
        <pages theme="">
        </pages>
 <authorization>
  <allow roles="ResellerUser, ResellerAdmin" />
  <deny users="*" />
</authorization>

Note to Page theme="" it is necessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文