在部署到生产环境时,如何限制对glimpse.axd的访问?
我使用了一个自定义的RuntimePolicy来确保在生产环境中没有启用glimpse,但是我希望确保用户不会同时访问axd。
如果我们使用来自asp.net的授权,那么我知道我可以在web.config中通过位置路径进行保护,但我不能使用此选项。
想法?
发布于 2013-09-12 23:04:41
Glimpse提供了几种不同的安全配置机制。
正如您所提到的,第一个是利用ASP.NET的内置安全功能。为此,您可以在web.config中添加一个<location>元素,如下所示:
<location path="glimpse.axd">
<system.web>
<authorization>
<deny users="*"/>
<allow roles="Admin"/>
</authorization>
</system.web>
</location>现在,只有管理员角色的用户才能访问Glimpse.axd。
巧合的是,路径不必是/Glimpse.axd,这只是一个默认设置。您可以通过对web.config进行一些更改,将HttpHandler的位置移动到只有您和您的团队知道的url
<!-- configure system.webServer and/or system.web depending on your ISS configuration -->
<system.webServer>
<handlers>
<add name="Glimpse" path="unknownLocation.axd" ... />
</handlers>
</system.webServer>
<system.web>
<httpHandlers>
<add path="unknownLocation.axd" ... />
</httpHandlers>
</system.web>
<!-- then just configure Glimpse -->
<glimpse defaultRuntimePolicy="On" endpointBaseUri="~/unknownLocation.axd">第二种方法是创建一个IRuntimePolicy。只要您从资源的ExecuteOn属性返回RuntimeEvent.ExecuteResource,运行时策略就可以保护对资源(通过Glimpse.axd提供)的访问。不幸的是,Glimpse被设计为忽略对默认资源(即Glimpse.axd)的请求的IRuntimePolicy。好消息是,您可以更改默认资源。方法如下:
服务定位器创建或更新一个类,以便它实现IServiceLocator.
web.config指向您的服务定位器实现。<glimpse defaultRuntimePolicy="On" endpointBaseUri="~/Glimpse.axd" serviceLocatorType="YourNamespace.GlimpseLocator, YourAssembly">
IResource。我将展示如何创建一个示例,它只是将用户重定向到常规配置页面(不再是默认资源),但您可以让它做任何您想做的事情。
/Glimpse.axd?n=glimpse_config将遵守您已有的所有IRuntimePolicy,并且无论如何都会将对Glimpse.axd的调用重定向到那里。代码如下:
// Create the ServiceLocator that is referenced in web.config
public class GlimpseLocator : IServiceLocator
{
public T GetInstance<T>() where T : class
{
if (typeof(T) == typeof(IResource))
return new SecurityResource() as T;
return null;
}
public ICollection<T> GetAllInstances<T>() where T : class
{
return null;
}
}
//Implementation of new default resource that just redirects
public class SecurityResource : IResource
{
public string Name
{
get { return "Security"; }
}
public IEnumerable<ResourceParameterMetadata> Parameters
{
get { return Enumerable.Empty<ResourceParameterMetadata>(); }
}
public IResourceResult Execute(IResourceContext context)
{
return new RedirectResourceResult("/Glimpse.axd?n=glimpse_config");
}
}
// Your custom runtime policy
public class CustomPolicy : IRuntimePolicy
{
public RuntimeEvent ExecuteOn
{
get { return RuntimeEvent.ExecuteResource; }
}
public RuntimePolicy Execute(IRuntimePolicyContext policyContext)
{
//Perform any logic you like and return RuntimePolicy.On or RuntimePolicy.Off
return RuntimePolicy.Off;
}
}现在,当用户转到Glimpse.axd时,他们会被重定向到Glimpse.axd?n=glimpse_config,它会显示标准的配置页面,或者*运行时策略不允许执行名为'glimpse_ config '.*的资源-这取决于您的IRuntimePolicy。
因此,正如我所说的,我们优化的用例是第一个,利用ASP.NET的内置安全机制。不过,Glimpse并不与该模型绑定,您只需跳过几个圈子就可以配置它的ATM。
在相关的注释中,我们将在Glimse2.0中使用greatly improving the configuration story,目前正在开发中。
发布于 2015-10-26 22:13:07
从Glimpse 1.7.0开始,他们添加了一种更好的方法来保护Glimpse.axd:
http://blog.getglimpse.com/2013/12/09/protect-glimpse-axd-with-your-custom-runtime-policy/
httpContext.User.IsInRole("Administrator")更改为与您相关的任何逻辑注意:ExecuteOn()中的RuntimeEvent.ExecuteResource可确保在您请求Glimpse.axd时运行此逻辑。
发布于 2014-09-25 07:51:46
我没有足够的名气来评论,但我认为有必要在nikmd23的彻底回答中从以下错误中拯救某人:
<location path="glimpse.axd">
<system.web>
<authorization>
<deny users="*"/><------------
<allow roles="Admin"/>
</authorization>
</system.web>
</location>箭头指向的问题是,在授权有机会允许roles="admin“之前,deny *将匹配所有用户。颠倒这个顺序(如下所示)以达到所需的功能。
参考:ASP.NET Forms Auth Allowing access to specific file in subdirectory when all others should be denied
<location path="glimpse.axd">
<system.web>
<authorization>
<allow roles="Admin"/>
<deny users="*"/>
</authorization>
</system.web>
</location>https://stackoverflow.com/questions/18752303
复制相似问题