I'm following this guide:
http://www.asp.net/web-api/overview/security/preventing-cross-site-request-forgery-(csrf)-attacks
I've added this snippet to the top of my index.cshtml
<script>
@functions{
public string TokenHeaderValue()
{
string cookieToken, formToken;
AntiForgery.GetTokens(null, out cookieToken, out formToken);
return cookieToken + ":" + formToken;
}
}
</script>
The problem is when I run the application and inspect the page - all I see is an empty script tag.
Adding this snippet to my $http request:
headers: {
'RequestVerificationToken': '@TokenHeaderValue()'
}
Does nothing but add the string '@TokenHeaderValue()' to the headers.
The entire app is a SPA using Angular. The only .cshtml file is the index which bootstraps Angular and includes styles, ect.
What am I missing here?
@functions {}
is for server--side c# code to be callable while rendering your view, which is why you see an empty script tag clientside. Where you expecting Javascript here? – David Tansey Oct 25 '15 at 18:43