I am developing a web API
app running using asp.net core2
and Angular
. The detailed development environment config is here.
I am trying to
I'm assuming you probably followed the documentation, but glossed over the pertinent bits. What you've done so far works only for Angular, because Angular's $http
will actually add the X-XSRF-TOKEN
header based on the XSRF-TOKEN
cookie. (Note, however, that even then, you've set your header as X-CSRF-TOKEN
, which won't actually work here. It needs to be X-XSRF-TOKEN
).
However, if you're not using Angular, you're responsible for setting the header yourself in your AJAX requests, which you likely are neglecting to do. In this case, you don't actually need to change any of the antiforgery token config (header names, setting cookies, etc.). You simply need to provide the header as RequestVerificationToken
. For example, with jQuery:
$.ajax({
...
headers:
{
"RequestVerificationToken": '@GetAntiXsrfRequestToken()'
},
...
});
That will work for JavaScript in view. If you need to do this in external JS, then you would need to set the cookie, so that you can get at the value from the cookie instead. Other than that, the same methodology applies.
If you simply want to change the header name, you can do so; you just need to change the RequestVerificationHeader
portion here to the same value.