简体   繁体   English

以编程方式确定路径是否受限制

[英]Programmatically determine if path is restricted

I am creating an HttpModule in ASP.NET 2. The module needs to perform a different logic based on whether or not the requested path is public or protected. 我在ASP.NET 2中创建一个HttpModule。模块需要根据请求的路径是公共路径还是受保护路径执行不同的逻辑。 The web.config sets authorization with the <system.web><authorization> tag and several <location> tags. web.config使用<system.web><authorization>标记和几个<location>标记设置授权。

Is there a way for the HttpModule to figure out if the path of the current request is protected or not? 有没有办法让HttpModule判断出当前请求的路径是否受到保护? I don't want to hard code the values in the code. 我不想硬编码代码中的值。

如果您使用标准authentication/authorization则可以使用CheckUrlAccessForPrincipal

UrlAuthorizationModule.CheckUrlAccessForPrincipal(virtualPath, user, verb);

There's no direct way to look at a filesystem entry and get your effective permissions on it. 没有直接的方法来查看文件系统条目并获得它的有效权限。 And computing the effective permission set on a file or directory is...complicated (that would be a polite way of putting it). 计算文件或目录上的有效权限集是......复杂的(这将是一种礼貌的方式)。

Seems like that would be a fairly obvious piece of information that the System.IO classes ought to provide, but evidently the CLR team didn't think so. 看起来这将是System.IO类应该提供的相当明显的信息,但显然CLR团队并不这么认为。 I think part of the problem is the inherent race condition. 我认为问题的一部分是固有的竞争条件。 The permissions on a given object are dynamic and could conceivably change at any time. 给定对象的权限是动态的,可以随时改变。 They could even change between your permissions check and the actual accessing of the object, resulting in an exception being raised. 它们甚至可以在您的权限检查和对象的实际访问之间进行更改,从而导致异常被引发。

These questions have some help: 这些问题有一些帮助:

The easiest way is to demand the permissions you want, catch the exception if you don't have them and use that to return a bool yes/no value: 最简单的方法是要求你想要的权限,如果你没有它们就捕获异常并使用它来返回bool是/否值:

// you'll need access to the namespace namespace System.Security.Permissions
public bool HasAccess( string path , FileIOPermissionAccess accessDesired )
{
  bool isGranted ;

  try
  {
    FileIOPermission permission = new FileIOPermission( accessDesired , path ) ;

    permission.Demand() ;

    isGranted = true ;

  }
  catch
  {
    isGranted = false ;
  }

  return isGranted ;
}

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

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