Skip to content

Instantly share code, notes, and snippets.

@stefanhendriks
Created May 11, 2016 18:32
Show Gist options
  • Save stefanhendriks/f7fe45d2e3bce09d002f3328d44915bc to your computer and use it in GitHub Desktop.
Save stefanhendriks/f7fe45d2e3bce09d002f3328d44915bc to your computer and use it in GitHub Desktop.
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