Exemple #1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="context"></param>
 /// <param name="output"></param>
 public override void Process(TagHelperContext context, TagHelperOutput output)
 {
     if (Enabled)
     {
         var           nonce       = CSP.CreateNonce();
         var           httpContext = _httpContextAccessor.HttpContext;
         List <string> nonceList;
         var           key = string.Empty;
         if (string.Equals(context.TagName, "script", StringComparison.OrdinalIgnoreCase))
         {
             key = SecurityHeadersAttribute.CSP_SCRIPT_NONCE_HTTPCONTEXT_KEY;
         }
         else if (string.Equals(context.TagName, "style", StringComparison.OrdinalIgnoreCase))
         {
             key = SecurityHeadersAttribute.CSP_STYLE_NONCE_HTTPCONTEXT_KEY;
         }
         if (httpContext.Items.ContainsKey(key))
         {
             nonceList = (List <string>)httpContext.Items[key];
         }
         else
         {
             nonceList = new List <string>();
             httpContext.Items.Add(key, nonceList);
         }
         if (!output.Attributes.ContainsName("nonce"))
         {
             output.Attributes.Add("nonce", nonce);
             nonceList.Add(nonce);
         }
     }
 }