Log in with code
This page was created by Hans.karlsen on 2017-06-11. Last edited by Stephanie on 2025-02-20.
Turnkey use MVC for the login form. This form make use of the __RequestVerificationToken that helps MVC avoid attacks where an old posted form is used again.
You will need to supply a valid RequestVerificationToken when logging in from code.
The easiest way to get a valid RequestVerificationToken is to screen scrape it from the login page.
The code below download the Login page, finds the RequestVerificationToken.
Then the code make a post with the needed parameters for login including the screen scraped __RequestVerificationToken
private void Button_Click_1(object sender, RoutedEventArgs e) { var client = new HttpClient(); var loginform = client.GetAsync("https://raptor3ny/TurnkeyWebAppGeneric/Account/Login").Result; var loginformcontent = loginform.Content.ReadAsStringAsync().Result; var part1=loginformcontent.Substring(loginformcontent.IndexOf("<input name=\"__RequestVerificationToken\""), 1000); part1 = part1.Substring(part1.IndexOf("value=")); part1 = part1.Substring(part1.IndexOf('"') + 1); part1 = part1.Substring(0,part1.IndexOf('"')); var content = new MultipartFormDataContent(); content.Add(new StringContent("hans@karlsen.se"), "EMail"); content.Add(new StringContent("123456"), "Password"); content.Add(new StringContent("false"), "RememberMe"); content.Add(new StringContent(part1), "__RequestVerificationToken"); var result = client.PostAsync("https://raptor3ny/TurnkeyWebAppGeneric/Account/Login", content).Result; if (result.StatusCode == System.Net.HttpStatusCode.OK) { // Login successfull } }