Attempting to retrieve session token results in CERT_AUTH_REQUIRED

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Roon
    Junior Member
    • Apr 2012
    • 4

    #1

    Attempting to retrieve session token results in CERT_AUTH_REQUIRED

    I posted this in the .NET forum but thought here might be a better option.

    I am currently trying to use the new API and am trying to get my C# code to retrieve the session token. I have verified that I have uploaded the correct certificate to Betfair by running both the curl and python samples and they return the correct session token.

    I have been scouring the web and everyone says the method below is how to send a client certificate through an HTTP POST. I have replaced all sensitive data with XXXXX. When I execute the code, i get the JSON returned {"loginStatus":"CERT_AUTH_REQUIRED"}

    I would appreciate it if someone could give me some direction on this one, as I cannot find any sample c# code for this part of the application on the betfair site, only java and python.

    const string host = "https://identitysso-api.betfair.com/api/certlogin";

    X509Certificate2 certificate = new X509Certificate2(@"client-2048.p12", "XXXXXX", X509KeyStorageFlags.PersistKeySet);

    ServicePointManager.ServerCertificateValidationCal lback += ServerCertificateValidationCallback;

    HttpWebRequest req = (HttpWebRequest) WebRequest.Create(host);

    req.Method = "POST";
    req.ContentType = "application/x-www-form-urlencoded";
    req.Headers.Add("X-Application", "XXXXXXXXXXXXXX");

    StringBuilder postData = new StringBuilder();
    postData.Append(String.Format("username={0}&", "XXXXXXXXXX"));
    postData.Append(String.Format("password={0}", "XXXXXXXXXXX"));
    Console.WriteLine("Post Data: " + postData.ToString());
    ASCIIEncoding ascii = new ASCIIEncoding();
    byte[] postBytes = ascii.GetBytes(postData.ToString());

    req.ContentLength = postBytes.Length;
    Stream postStream = req.GetRequestStream();
    postStream.Write(postBytes, 0, postBytes.Length);
    postStream.Flush();
    postStream.Close();

    req.ClientCertificates.Add(certificate);

    WebResponse resp = null;
    try
    {
    resp = req.GetResponse();
    }
    catch (Exception ex)
    {
    }

    Stream stream = resp.GetResponseStream();
    StreamReader sr = new StreamReader(stream, Encoding.UTF8);

    string result = sr.ReadToEnd();

    Console.WriteLine(result);

    return result;
  • Ernests Gulbis
    Junior Member
    • Aug 2009
    • 3

    #2
    You're adding a certificate after closing the request stream.

    Try adding a certificate right after creating a request object.

    Code:
    HttpWebRequest req = (HttpWebRequest) WebRequest.Create(host);
    req.ClientCertificates.Add(certificate);

    Comment

    • Roon
      Junior Member
      • Apr 2012
      • 4

      #3
      Excellent, that is exactly correct. Pretty stupid of me not to pick that one up.

      Thanks Ernests.

      Comment

      • brentonMcS
        Junior Member
        • May 2013
        • 2

        #4
        Did you get this working with that change? I copied and pasted your example, fixed up where the cert was being added and still got the same login Status response back.

        I'm also getting the same error with my example below:

        private const string host = "https://identitysso-api.betfair.com/api/certlogin";

        public BetFairApi()
        {
        _httpHandler = new WebRequestHandler();

        var cert = new X509Certificate2(CertLocation, CertKey, X509KeyStorageFlags.PersistKeySet);
        _httpHandler.ClientCertificates.Add(cert);

        _httpClient = new HttpClient(_httpHandler);

        var payload = new StringContent(string.Format("username={0}&password ={1}", userName, password));
        payload.Headers.ContentType.MediaType = "application/x-www-form-urlencoded";

        _httpClient.DefaultRequestHeaders.Add("X-Application", AppKey);

        var result = _httpClient.PostAsync(host, payload).Result;

        }

        Comment

        • brentonMcS
          Junior Member
          • May 2013
          • 2

          #5
          Turns out if you have Fiddler Running with it's fake CA so you can decrypt HTTPS traffic it causes the certificate to not work!

          Comment

          Working...
          X