Skip to content

Instantly share code, notes, and snippets.

@emrekizildas
Created October 5, 2019 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emrekizildas/beaa9f7e31de9dccc1c11c2d1b050e76 to your computer and use it in GitHub Desktop.
Save emrekizildas/beaa9f7e31de9dccc1c11c2d1b050e76 to your computer and use it in GitHub Desktop.
app.Use((context, next) =>
{
var rng = new RNGCryptoServiceProvider();
var nonceBytes = new byte[32];
rng.GetBytes(nonceBytes);
var nonce = Convert.ToBase64String(nonceBytes);
context.Items.Add("ScriptNonce", nonce);
context.Response.Headers.Add("Content-Security-Policy",
new[] { string.Format("script-src 'self' 'nonce-{0}'", nonce) });
return next();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment