... However, I need to add the nonce to another attri" /> ... However, I need to add the nonce to another attri" /> ... However, I need to add the nonce to another attri"/>

Add nonce generated by NWebSec to another attribute

1.1k Views Asked by At

I'm using NWebSec to add nonces to script tags, e.g.

<script nws-csp-add-nonce="true">
    ...
</script>

However, I need to add the nonce to another attribute, so need to do something like this:

<script nws-csp-add-nonce="true" data-nonce="@Html.CspNonce()">
    ...
</script>

Is this possible with NWebSec, or is the generated nonce not accessible in any way?

For context, this is to try to get Google tag manager to propagate the nonce to custom HTML scripts.

Edit

The suggested solution below seems to work. So I needed to add this to the GTM script:

var gtmScript = document.getElementById('gtmscript');
gtmScript.setAttribute('data-nonce', gtmScript.nonce);

Which takes the nonce and adds it to a data-nonce attribute. Not the nicest solution. I'd much prefer having access to the nonce in the backend.

0

There are 0 best solutions below