diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/NonceHtmlHelper.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/NonceHtmlHelper.cs new file mode 100644 index 0000000000..42ce396386 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/NonceHtmlHelper.cs @@ -0,0 +1,20 @@ +using Microsoft.AspNetCore.Html; +using Microsoft.AspNetCore.Mvc.Rendering; +using Microsoft.AspNetCore.Mvc.ViewFeatures; + +namespace Volo.Abp.AspNetCore.Security; + +public static class NonceHtmlHelper +{ + public static IHtmlContent ScriptWithNonce(this HtmlHelper htmlHelper, string scriptUrl) + { + var tagBuilder = new TagBuilder("script"); + tagBuilder.Attributes.Add("src", scriptUrl); + if(htmlHelper.ViewContext.HttpContext.Items.TryGetValue(AbpAspNetCoreConsts.ScriptNonceKey, out var nonce) && nonce is string nonceString && !string.IsNullOrEmpty(nonceString)) + { + tagBuilder.Attributes.Add("nonce", nonceString); + } + + return tagBuilder; + } +} \ No newline at end of file