Create a gist now

Instantly share code, notes, and snippets.

AntiForgeryHelper class to read token from request (Asp.Net core)
public class AntiForgeryHelper
{
public static string ExtractAntiForgeryToken(string htmlResponseText)
{
if (htmlResponseText == null) throw new ArgumentNullException("htmlResponseText");
System.Text.RegularExpressions.Match match = Regex.Match(htmlResponseText, @"\<input name=""__RequestVerificationToken"" type=""hidden"" value=""([^""]+)"" \/\>");
return match.Success ? match.Groups[1].Captures[0].Value : null;
}
public static async Task<string> ExtractAntiForgeryToken(HttpResponseMessage response)
{
string responseAsString = await response.Content.ReadAsStringAsync();
return await Task.FromResult(ExtractAntiForgeryToken(responseAsString));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment