RedditPP Mascot

RedditPP

r/learnprogramming • … • u/Beacon_On_The_Moors

C# - Is it possible to dynamically hash the result of an extension method (@Html.ValidationSummary())?

I’m a new developer and my first job has put me into a full stack development role. I’m currently going through one of our website’s code and moving unsafe inline css and js styles/scripts into separate master css and js files to be compliant with content security policy.

I’m having trouble with a ValidationSummary module result. It’s building an html result that includes an unordered list of errors with display:none styling.

Honestly C# is new to me and I’ve been drinking from a firehose. I'm still trying to learn about controllers, modules, models, lambda and linq functions which are spread throughout this code. I tried doing some things I came across through research like making a new controller with SHA256 and setting validation summary result to a string and then @ viewbag.dynamichash in the cshtml file. That's giving me an a CS1955 error. I also tried setting the result to a string variable in the cshtml file and overriding the result with a new class but that also didn't work.

I’m just confused how to deal with ValidationSummary and how I can hash it to be compliant with CSP without having to hardcode every unique output into the controller file. My lead doesn't want to use nonce and that's all I can find about dynamic generation.

Cshtml snippet:

<form action="@Url.Action(" LoginPreAuth")" method="post" class="ym-form ym-full">

@Html.AntiForgeryToken()

<h2 class="ym-fbox-heading">Log In</h2>

<fieldset>

<legend>Credentials</legend>

@*@Html.ValidationSummary(false, "Please correct the following errors:")*@

@{ String DynamicHash = @Html.ValidationSummary(false, “Please correct the following errors:”) } // tried making a class for this in css file to override style being brought in from ValidationSummary()

ViewBag.DynamicHash // Tried calling from controller with line above crossed out

@Html.Partial("_FormLegend")

<div class="ym-fbox-text">

@Html.LabelFor(m => m.UserName, new { @id = "LB_UserName", @class = "required" })

@Html.TextBoxFor(m => m.UserName, new { autocomplete = "off" })

</div>

</fieldset>

<div class="ym-fbox-button">

<button type="submit" class="ym-button ym-next">Log in</button>

</div>

</form>

Experimental controller Code:

namespace ExambleSite.Web.Controllers { public class DynamicHashController : Controller { // Calculate dynamic hash private string CalculateHash(string input)

{ using (SHA256 sha256 = SHA256.Create()) { byte[] hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(input));

return Convert.ToBase64String(hashBytes); } }

// GET: DynamicHash public ActionResult Index()

{ string dynamicErrorList = ValidationSummary(false, "Please correct the following errors:"); string dynamicHash = CalculateHash(dynamicErrorList);

ViewBag.DynamicHash = dynamicHash;

return View(); } } }

Error CS1955: Non-invocable member 'ValidationSummary' cannot be used like a method.

&#x200B;

— 0