asp.net-mvc – 禁用生产中的Glimpse和glimpse.axd

如何在部署到生产环境时限制对glimpse.axd的访问?

我正在使用自定义的RuntimePolicy来确保在生产中未启用一瞥,但是我想确保用户不能访问axd.

如果我们使用asp.net的授权,那么我明白我可以通过web.config中的位置路径进行保护,但这个选项对我来说是不可用的.

想法?

解决方法

Glimpse提供了几种不同的安全配置机制.

正如你所说,第一个是利用ASP.NET的内置安全功能.为此,在您的web.config中,您可以添加< location>元素,像这样:

<location path="glimpse.axd">
  <system.web>
    <authorization>
      <deny users="*"/>
      <allow roles="Admin"/>
    </authorization>
  </system.web>
</location>

而现在只有Admin角色中的用户才能访问Glimpse.axd.

巧合的是,路径不一定是/Glimpse.axd,这只是认设置.您可以通过对您的web.config进行一些更改,将HttpHandler的位置移动到只有您和您的团队已知的网址:

<!-- 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指向您的服务定位器实现.

< glimpse defaultRuntimePolicy =“On”endpointBaseUri =“〜/ Glimpse.axd”serviceLocatorType =“YourNamespace.GlimpseLocator,YourAssembly”>
>现在,Glimpse知道你的定位器,并会要求它所需的任何类型,包括认资源.
>实现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的内置安全机制.瞥见并不是绑在那个模型上,你只需跳过几个环来配置自动取款机.

在相关的说明中,我们将在07年7月在Glimpse 2.0,目前正在进行中.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....