简体   繁体   English

如何在 https 站点的 asp.net Core 2.2 项目中启用压缩并避免诸如 CRIME 和 BREACH 之类的问题

[英]How to enable compression in asp.net Core 2.2 project for https site and avoid problems like CRIME and BREACH

I have a web application that is written using C# on the top of ASP.NET Core 2.2 MVC framework.我有一个 web 应用程序,它是使用 ASP.NET Core 2.2 MVC 框架顶部的 C# 编写的。

My application displays lots of images on every request which makes load time high and increase bandwidth usage.我的应用程序在每个请求上显示大量图像,这使得加载时间变长并增加了带宽使用。 To improve page load and bandwidth usage, I want to compress the HTTP response using Brotli and Gzip formats.为了改善页面加载和带宽使用,我想使用BrotliGzip格式压缩 HTTP 响应。

Luckily Microsoft has a package that uses middleware to compress the HTTP response for me called Microsoft.AspNetCore.ResponseCompression .幸运的是,微软有一个 package ,它使用中间件为我压缩 HTTP 响应,称为Microsoft.AspNetCore.ResponseCompression From the package official documentation , compression for secured websites is disabled by default.从 package 官方文档中,默认情况下禁用安全网站的压缩。

Using compression with dynamically generated pages can lead to security problems such as the CRIME and BREACH attacks.对动态生成的页面使用压缩可能会导致安全问题,例如CRIMEBREACH攻击。

My question how can I compress the response while avoiding security issues?我的问题是如何在避免安全问题的同时压缩响应?

Here is how I set up the Microsoft.AspNetCore.ResponseCompression package这是我如何设置Microsoft.AspNetCore.ResponseCompression package

public void ConfigureServices(IServiceCollection services)
{
    services.AddResponseCompression(options =>
    {
        options.Providers.Add<BrotliCompressionProvider>();
        options.Providers.Add<GzipCompressionProvider>();
        options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
        new[] { "image/svg+xml", "image/jpeg" });
        options.EnableForHttps = true;
    });

    ...
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseResponseCompression();
    ...
}

The above setup will apply compression for the following MIMI types上述设置将为以下 MIMI 类型应用压缩

  • application/javascript应用程序/javascript
  • application/json应用程序/json
  • application/xml应用程序/xml
  • text/css文本/CSS
  • text/html文本/html
  • text/json文本/json
  • text/plain文本/纯文本
  • text/xml文本/xml
  • image/svg+xml图片/svg+xml
  • image/jpeg图片/JPEG

In addition, you may also "compress" image prior to sending over, for example, using:此外,您还可以在发送之前“压缩”图像,例如,使用:

    private byte[] CompressImageToQuality(System.Drawing.Image image, int quality)
    {
            EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, quality); 
            ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
            EncoderParameters encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = qualityParam;
            using (var stream = new System.IO.MemoryStream())
            {
                image.Save(stream, jpegCodec, encoderParams);
                return stream.ToArray();
            }
    }

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

相关问题 如何避免 ASP.NET Core MVC 中的 paypal 安全漏洞 - How to avoid paypal security breach in ASP.NET Core MVC 如何为 ASP.NET Core 2.2 项目中的子域正确启用 CORS? - How to correctly enable CORS for a subdomain in an ASP.NET Core 2.2 project? asp.net 核心 2.2 中的响应压缩不起作用 - Response compression in asp.net core 2.2 not working 避免在Asp.NET Core 2.2 + IdentityServer中使用会话cookie - Avoid session cookies in Asp.NET Core 2.2 + IdentityServer 如何在 Asp.Net Core 2.2 中在运行时禁用/启用身份验证? - How do I disable/enable authentication at runtime in Asp.Net Core 2.2? ASP.NET linux 上的核心 2.2 项目未找到卫星资源 - ASP.NET Core 2.2 project on linux not finding satelite resources 如何在Core .NET 2.2项目中启用Facebook身份验证? - How to enable facebook authentication in Core .NET 2.2 project? 如何将Google Coto Stoage作为文件提供者链接到ASP.NET Core 2.2项目? - How to link Google Could Stoage into ASP.NET Core 2.2 project as file provider? 如何将位于应用程序部分 (DLL) 中的部分视图呈现到主 asp.net 核心 2.2 项目中 - How to render a partial view located in an application part (DLL) into main asp.net core 2.2 project 如何在 ASP.NET Core 2.2 项目中增加或设置上传媒体文件大小无限制 - How to increase or set the upload media file size unlimited in ASP.NET Core 2.2 project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM