source

.net core 2.0에서 x-powered-by 헤더를 삭제하는 방법

gigabyte 2023. 4. 24. 23:27
반응형

.net core 2.0에서 x-powered-by 헤더를 삭제하는 방법

이 미들웨어를 사용하려고 했습니다.

public class SecurityHeadersMiddleware
{
    private readonly RequestDelegate next;

    public SecurityHeadersMiddleware(RequestDelegate next)
    {
        this.next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        context.Response.OnStarting(state =>
        {
            var ctx = (HttpContext)state;

            if (!ctx.Response.Headers.ContainsKey("Arr-Disable-Session-Affinity"))
            {
                ctx.Response.Headers.Add("Arr-Disable-Session-Affinity", "True"); // Disables the Azure ARRAffinity cookie
            }

            if (ctx.Response.Headers.ContainsKey("Server"))
            {
                ctx.Response.Headers.Remove("Server"); // For security reasons
            }

            if (ctx.Response.Headers.ContainsKey("x-powered-by") || ctx.Response.Headers.ContainsKey("X-Powered-By"))
            {
                ctx.Response.Headers.Remove("x-powered-by");
                ctx.Response.Headers.Remove("X-Powered-By");
            }

            if (!ctx.Response.Headers.ContainsKey("X-Frame-Options"))
            {
                ctx.Response.Headers.Add("X-Frame-Options", "DENY");
            }

            return Task.FromResult(0);
        }, context);

        await next(context);
    }
}

x-powered-by는 응답 헤더에 asp.net로 표시됩니다.

IIS의 일부인 Request Filtering 모듈을 사용하면 이러한 헤더를 쉽게 제거할 수 있는 것으로 알고 있습니다.

헤더를 삭제하려면 다음 내용을 포함한 web.config 파일을 사이트에 저장해야 합니다.

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <!-- To customize the asp.net core module uncomment and edit the following section. 
  For more info see https://go.microsoft.com/fwlink/?linkid=838655 -->

  <system.webServer>
    <handlers>
      <remove name="aspNetCore"/>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
    <httpProtocol>
      <customHeaders>
        <remove name="X-Powered-By" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>


</configuration>

이 web.config를 네트워크 코어 애플리케이션의 루트 폴더에 추가합니다.

그런 다음 x-powered-by 헤더를 삭제합니다.

결과는 다음과 같습니다.

여기에 이미지 설명 입력

  • @Brando Zhang 답변 외에 "Server:응답 헤더로부터의 Kestrel:

-.NET 코어 1

 var host = new WebHostBuilder()
        .UseKestrel(c => c.AddServerHeader = false)
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

- NET Core 2

WebHost.CreateDefaultBuilder(args)
               .UseKestrel(c => c.AddServerHeader = false)
               .UseStartup<Startup>()
               .Build();

ASP에 web.config 파일을 작성하지 않는 경우.NET Core 솔루션에서는, 이 솔루션을 삭제할 수 있습니다.X-Powered-ByIIS Manager의 헤더.

를 클릭합니다.<ServerName> --> HTTP Response Headers --> X-Powered-By선택해주세요Remove액션.

IIS

서버상의 모든 Web 사이트의 헤더가 삭제됩니다.애초에 왜 그 정보를 공유하려고 합니까?

위의 답변 대신 구성 변환을 사용할 수 있습니다.그렇게 하면web.config는 dotnet publisher sdk를 통해 계속 생성되지만 헤더 삭제 등의 특정 태그와 혼합될 수 있습니다.

프로젝트의 루트에 새로운 것을 작성web.Release.config다음과 같이 파일합니다.

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <location>

    <!-- To customize the asp.net core module uncomment and edit the following section. 
    For more info see https://go.microsoft.com/fwlink/?linkid=838655 -->
    <system.webServer>
      <httpProtocol xdt:Transform="InsertIfMissing">
        <customHeaders>
          <remove name="X-Powered-By" />
        </customHeaders>
      </httpProtocol>
    </system.webServer>

  </location>
</configuration>

이것은 변환 파일이며 실제 파일이 아닙니다.web.config파일.

언급URL : https://stackoverflow.com/questions/45882715/how-to-remove-x-powered-by-header-in-net-core-2-0

반응형