From 94a1af7caa6c1b07eb5406f524507c3a8850d623 Mon Sep 17 00:00:00 2001 From: Salih Date: Thu, 4 May 2023 16:51:37 +0300 Subject: [PATCH] Create NonceHtmlHelper.cs --- .../AspNetCore/Security/NonceHtmlHelper.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/NonceHtmlHelper.cs 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