Requestverification

In header

@{
    ViewData["Title"] = "Titel";
}
@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
@functions{
    public string GetAntiXsrfRequestToken()
    {
        return Xsrf.GetAndStoreTokens(Context).RequestToken;
    }
}

in formulier

...
        <input type="hidden" id="RequestVerificationToken" name="RequestVerificationToken" value="@GetAntiXsrfRequestToken()">
...

in script

function Verzendformulier() {
    $.ajax({
        type: "POST",
        data: data,
        beforeSend: function(xhr){xhr.setRequestHeader('RequestVerificationToken', document.getElementById('RequestVerificationToken').value);},
        url: url,
        success: function () {
                functieNaPost()
            }
    });
}

 

Categories: , ,

Related Article