@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));
}
See Question&Answers more detail:os