简体   繁体   中英

login to ajax web page from c# code

i'm trying to log in a site with username + password through ac# code. i found out that it uses Ajax to authenticate...

how should i implement such login ? the elements in the web page doesn't seem to have an "id"...

i tried to implement it using HtmlAgilityPack but i don't think this is the correct direction... i can't simulate a click button since i don't find "id" for the button.

if (tableNode.Attributes["class"].Value == "loginTable")
{   
    var userInputNode =
        tableNode.SelectSingleNode("//input[@data-logon-popup-form-user-name-input='true']");
    var passwordInputNode =
        tableNode.SelectSingleNode("//input[@data-logon-popup-form-password-input='true']");

    userInputNode.SetAttributeValue("value", "myemail@gmail.com");
    passwordInputNode.SetAttributeValue("value", "mypassword");

    var loginButton = tableNode.SelectSingleNode("//div[@data-logon-popup-form-submit-btn='true']");                
}

This question is quite broad but I'll help you in the general direction:

  1. Use Chrome DevTools (F12) => Network tab => Check the "Preserve Log". An alternative could be Fiddler2
  2. Login manually and look at the request the AJAX sends. Save the endpoint (the URL) and save the Body of the request (the Json data that's in the request with username and password)
  3. Do the post directly in your C# code and forget about HtmlAgilityPack unless you need to actually get some dynamic data from the page, but that's rarely the case
  4. Login with something like this code snippet: POSTing JSON to URL via WebClient in C#
  5. Now you're logged in. You usually receive some data from the server when you're logging in, so save it and use it for whatever you want to do next. I'm guessing it might have some SessionId or some authentication token that your future requests will need as a parameter to prove that you're actually logged in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM