Sample C# code for Interactive login via webpage

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • geoffw123
    Senior Member
    • Mar 2014
    • 250

    #1

    Sample C# code for Interactive login via webpage

    Hi

    Related to my other post about Betfair breaking my ability to login using the API. I thought I would look into the alternative interactive login method via a webpage. In principle you have to add a web browser component to your App and open the Betfair login page for the user to fill in username and password.
    Then when they submit that form, instead of redirecting to the Betfair website, you intercept the call and grab the Session token and cancel the redirection.

    I was hoping it might be not too complex to implement that in a sample test program, but so far I cant get it to work.

    Does anyone know of sample code to do that on the web somewhere, or maybe have done it and be willing to share the code ?

    Thanks

  • WTPooh
    Member
    • May 2012
    • 88

    #2
    https://github.com/betfair/API-NG-sa...ractive-cSharp

    A quick note on this code: You don't need to enter Application Key. You can navigate directly to https://identitysso.betfair.com/view/login

    Comment

    • geoffw123
      Senior Member
      • Mar 2014
      • 250

      #3
      HI

      Thanks for the link. I had a quick glance at this code, from what I understand it looks like it is the conventional Betfair API login technique.

      That is what I have been using for years without a problem until Friday. For me and some other users Betfair managed to break this login that they still havent managed to fix 3 days later. So I was looking for sample code, that actually opens the real Betfair login webpage so the user can enter their password and username. This sample code needs to then grab the redirection when they press enter to snag the SessionId. This method for login is still working for me despite the API login being broken.

      For anyone else looking for Sample API code, this link may be a bit more useful than the link quoted above, as it is recent and works fine with Visual Studio 2022, its a Winform App and has a bit more functionality showing how to get things setup and working.
      https://github.com/geoffw123/API-NG-Basic-WinForm-App

      Comment

      • WTPooh
        Member
        • May 2012
        • 88

        #4
        Hi
        This sample code does exactly what you want. It opens real Betfair login page (https://identitysso.betfair.com/view/login) where you can enter your username and password.

        Comment

        • geoffw123
          Senior Member
          • Mar 2014
          • 250

          #5
          Hi

          Ah right, apologies for not spotting that, I didnt spend long reading the code as I saw that it was mandatory to pass in the session token to the main function as an argument. This is what I am trying to figure out in the first place, so it wasnt going to be very helpful for my specific need.

          Anyway, I have been working on my own sample version of how to snag the ssoid from the login webpage. I have just got this working a few minutes ago, so its sorted now thanks. My sample code to do this was surprisingly small. Just 40 lines of C# code in the end.

          Your note was helpful regarding the https://identitysso.betfair.com/view/login​ url, I had initially assumed I needed to use my product key in the URL, I have removed the key now and used that link it still worked fine

          If anyone is interested in this 40 line sample Winform App to get the session token SSOID let me know in this thread and I can probably put it up on Github.

          Thanks for the assistance.

          Geoff


          Comment

          • WTPooh
            Member
            • May 2012
            • 88

            #6
            Hi

            You dont need to pass session token or app key to this class. Here is the part of the class you need to login:

            Code:
            public class Login
            {
            
                public string sessionToken;
                public bool loggedIn;
            
                private const string logonURL = "https://identitysso.betfair.com/view/login";
            
                private void Login_Load(object sender, EventArgs e)
                {
                    webBrowser1.ScriptErrorsSuppressed = true;
                    webBrowser1.Url = new Uri(logonURL);
                    webBrowser1.Navigating += WebBrowser1_Navigating;
                }
            
                private void ParseCookie(string cookie)
                {
                    const string ssoid = "ssoid=";
                    if (!loggedIn & cookie.IndexOf("loggedIn=true;") >= 0)
                    {
                        int ssoIdIndex = cookie.IndexOf(ssoid);
            
                        if (ssoIdIndex >= 0)
                        {
                            sessionToken = cookie.Substring(ssoIdIndex + ssoid.Length);
                            sessionToken = sessionToken.Substring(0, sessionToken.IndexOf(';'));
                            loggedIn = true;
                        }
                    }
                }
            
                private void WebBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
                {
                    if (!loggedIn)
                    {
                        string cookie = webBrowser1.Document?.Cookie;
                        if (cookie != null) ParseCookie(cookie);
                        if (loggedIn) Close();
                    }
                }
            }​

            Example of usage:
            Code:
            var l = new Login();
            l.ShowDialog();
            if (l.loggedIn)
            {
               token = l.sessionToken;
            }
            else
            {
            
            }​
            All this code was created by converter from VB.NET to С#. So it may contain some minor errors.​

            Comment

            • geoffw123
              Senior Member
              • Mar 2014
              • 250

              #7
              Hiya

              Thanks for the code snippet, it looks almost identical to what I came up with today.

              Regards

              Comment

              Working...
              X