diff --git a/rules/S5146/csharp/how-to-fix-it/blazor.adoc b/rules/S5146/csharp/how-to-fix-it/blazor.adoc new file mode 100644 index 0000000000..332586001a --- /dev/null +++ b/rules/S5146/csharp/how-to-fix-it/blazor.adoc @@ -0,0 +1,50 @@ +== How to fix it in Blazor + +=== Code examples + +include::../../common/fix/code-rationale.adoc[] + +==== Noncompliant code example + +[source,csharp,diff-id=2,diff-type=noncompliant] +---- +@page "/" +@inject NavigationManager Navigation + +@code { + [SupplyParameterFromQuery] + private String url {get ; set; } + + protected override void OnInitialized() { + Navigation.NavigateTo(url); + } +} +---- + +==== Compliant solution + +[source,csharp,diff-id=2,diff-type=compliant] +---- +@page "/" +@inject NavigationManager Navigation + +@code { + [SupplyParameterFromQuery] + private String url {get ; set; } + + private readonly string[] allowedUrls = { "/", "/login", "/logout" }; + + protected override void OnInitialized() { + if (allowedUrls.Contains(url)) + { + Navigation.NavigateTo(url); + } + } +} +---- + +include::../../common/fix/how-does-this-work.adoc[] + +=== Pitfalls + +include::../../common/pitfalls/starts-with.adoc[] diff --git a/rules/S5146/csharp/rule.adoc b/rules/S5146/csharp/rule.adoc index af3d8431e1..43a1f5c303 100644 --- a/rules/S5146/csharp/rule.adoc +++ b/rules/S5146/csharp/rule.adoc @@ -8,6 +8,8 @@ include::../impact.adoc[] include::how-to-fix-it/dotnet.adoc[] +include::how-to-fix-it/blazor.adoc[] + == Resources include::../common/resources/standards.adoc[]