@Html.AntiForgeryToken() renders hidden input
<input name="__RequestVerificationToken" type="hidden" value="GuiNIwhIJZjINHhuS_8FenaFDXIiaE" />
How can I get token value only? Without ugly code like this:
public static IHtmlString AntiForgeryTokenValue(this HtmlHelper htmlHelper) { var field = htmlHelper.AntiForgeryToken().ToHtmlString(); var beginIndex = field.IndexOf("value=\"") + 7; var endIndex = field.IndexOf("\"", beginIndex); return new HtmlString(field.Substring(beginIndex, endIndex - beginIndex)); }
If you use the AntiForgery. GetTokens API, this method will return the raw tokens instead of generating an HTML snippet. The parameters to this method are: oldCookieToken: If the request already contains an anti-CSRF cookie token, provide it here.
AntiForgeryToken()Generates a hidden form field (anti-forgery token) that is validated when the form is submitted.
Validates that input data from an HTML form field comes from the user who submitted the data. Obsolete. Validates that input data from an HTML form field comes from the user who submitted the data and lets callers specify additional validation details.
Anti-Forgery Tokens The server includes two tokens in the response. One token is sent as a cookie. The other is placed in a hidden form field. The tokens are generated randomly so that an adversary cannot guess the values.
The anti-CSRF capabilities of MVC actually depend on two tokens: one is a hidden form element, and the other is a cookie. So the Html.AntiForgeryToken() helper doesn't just return an HTML snippet. It also has a side effect of setting this cookie. Note that the cookie value and the form value are not equal since they each encode different pieces of information.
If you use the AntiForgery.GetTokens API, this method will return the raw tokens instead of generating an HTML snippet. The parameters to this method are:
If you use this API to generate cookie and form tokens manually, you'll need to call the corresponding overload of AntiForgery.Validate in order to validate the tokens.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With