加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-15 22:50:39 所属栏目:asp.Net 来源:网络整理
导读:如何在部署到生产环境时限制对glimpse.axd的访问? 我正在使用自定义的RuntimePolicy来确保在生产中未启用一瞥,但是我想确保用户不能访问axd. 如果我们使用asp.net的授权,那么我明白我可以通过web.config中的位置路径进行保护,但这个选项对我来说是不可用的.
如何在部署到生产环境时限制对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,目前正在进行中.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读