I have recently implemented AntiXSS (4.3.0) into my project, primarily to utilize @Encoder.JavaScriptEncode
as outlined in this resource.
After installing AntiXSS via Nuget, I added
encoderType="Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary"
to <httpRuntime
in the Web.config file.
Within my view file, I included the following line (enclosed with <script>
tags):
var userId = @Encoder.JavaScriptEncode(User.Identity.GetUserId());
I was expecting the output to be:
var userId = 'user-id';
However, it actually displays as:
var userId = 'user-id';
I suspect that this discrepancy is due to Razor attempting to sanitize the HTML, resulting in the single quotes being encoded as '
.
The proposed solution would be to encapsulate it in Html.Raw()
, although the guide I referenced did not mention that approach (instead opting to enclose the entire segment within single quotes in the JavaScript).
My question is - should I be required to use
@Html.Raw(Encoder.JavaScriptEncode(data))
, or is there an issue with my configuration?
Thank you!