Hi,
We have added Content-Security-Policy on web.config.
It is not working.
value="default-src 'self'; script-src 'self'; object-src 'none';" /> Regards, Pratik
Hi,
We have added Content-Security-Policy on web.config.
It is not working.
value="default-src 'self'; script-src 'self'; object-src 'none';" /> Regards, Pratik
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Cynthia SathuragiriRank #937 · 2.2K reputationPosted Sep 22, 2025, 2:16 AM EDT
In your web.config, under
Sandhiya PriyaRank #26 · 43.2K reputationPosted Oct 27, 2025, 4:51 AM EDT
Problem:
In classic ASP.NET (not ASP.NET Core), adding CSP headers using this syntax in
web.configoften doesn’t work:because:
IIS doesn’t always pass custom headers correctly when added under
in older framework versions.Certain middleware or modules (e.g., Forms Authentication or URL Rewrite) may overwrite or remove the header.
Correct Way (for ASP.NET Web Forms / MVC in web.config)
Add this inside:
Verify It Works
After saving
web.config, restart the app (or IIS), and then open the site in a browser.Check the response headers:
Open Developer Tools (F12) ? Network tab
Refresh your page
Select your main request and look under Response Headers
You should see:
If it’s missing, see the next step
If It Still Doesn’t Work
Add the header programmatically in Global.asax instead:
protected void Application_BeginRequest(object sender, EventArgs e) { HttpContext.Current.Response.Headers.Add( "Content-Security-Policy", "default-src 'self'; script-src 'self'; object-src 'none';" ); }This method guarantees that the CSP header is sent for every request.
Summary