0

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.

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah as per your answer i added headers but Content-Security-Policy values affecting my html content . What are the values for Content-Security-Policy header that won't be affecting my html and css content
How is the html content affected? This might be useful - blog.elmah.io/content-security-policy-in-asp-net-mvc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.