Home > Blockchain >  How to add http response headers in asp.net 5.0
How to add http response headers in asp.net 5.0

Time:11-05

My web application which build on .NET 5.0. How can I add below http response headers?

X-Frame-Options     

Content-Security-Policy  

X-XSS-Protection 

X-Content-Type-Options

Above http response headers are missing from web application.

CodePudding user response:

I find the easiest approach is in the Startup.cs file:

app.Use(async (context, next) =>
    {
        context.Response.Headers.Add("Header-Name", "Header-Value");
        await next();
    });

Make sure you add this call before before calling UseEndpoints, UseMvc etc.

If you prefer to add these in the web.config, it can be achieved by

<configuration>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Header-Name" value="Header-Value" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>
  • Related