I've just started using AntiXSS (4.3.0), mostly to use @Encoder.JavaScriptEncode
as described here.
I installed AntiXSS from Nuget, then added encoderType="Microsoft.Security.Application.AntiXssEncoder, AntiXssLibrary"
to <httpRuntime
in my Web.config.
In my view, I have the following line (within <script>
tags):
var userId = @Encoder.JavaScriptEncode(User.Identity.GetUserId());
Which I would expect to output
var userId = 'user-id';
but instead outputs:
var userId = 'user-id';
I assume this is happening because Razor is trying to sanitize the HTML, and thus encoding the singe quotes as '
.
The solution then would be to just wrap it in Html.Raw()
, but in the post I was following he never does that (instead wrapping the whole thing in single quotes within the Javascript).
My question is - are you supposed to need to call @Html.Raw(Encoder.JavaScriptEncode(data))
, or is there something wrong with my setup?
Thanks!