Created
May 11, 2016 18:32
-
-
Save stefanhendriks/f7fe45d2e3bce09d002f3328d44915bc to your computer and use it in GitHub Desktop.
AntiForgeryHelper class to read token from request (Asp.Net core)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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