Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8358295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:45:01+00:00 2026-06-09T10:45:01+00:00

I am trying to implement Facebook X_FACEBOOK_PLATFORM SASL mechanism so I could integrate Facebook

  • 0

I am trying to implement Facebook X_FACEBOOK_PLATFORM SASL mechanism so I could integrate Facebook Chat to my application over XMPP.

Here is the code:

  var ak = "my app id";
  var sk = "access token";
  var aps = "my app secret";
  using (var client = new TcpClient())
  {
    client.Connect("chat.facebook.com", 5222);
    using (var writer = new StreamWriter(client.GetStream())) using (var reader = new StreamReader(client.GetStream()))
    {
      // Write for the first time
      writer.Write("<stream:stream xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\" to=\"chat.facebook.com\"><auth xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\" mechanism=\"X-FACEBOOK-PLATFORM\" /></stream:stream>");
      writer.Flush();
      Thread.Sleep(500);
      // I am pretty sure following works or at least it's not what causes the error
      var challenge = Encoding.UTF8.GetString(Convert.FromBase64String(XElement.Parse(reader.ReadToEnd()).Elements().Last().Value)).Split('&').Select(s => s.Split('=')).ToDictionary(s => s[0], s => s[1]);
      var response = new SortedDictionary<string, string>() { { "api_key", ak }, { "call_id", DateTime.Now.Ticks.ToString() }, { "method", challenge["method"] }, { "nonce", challenge["nonce"] }, { "session_key", sk }, { "v", "1.0" } };
      var responseString1 = string.Format("{0}{1}", string.Join(string.Empty, response.Select(p => string.Format("{0}={1}", p.Key, p.Value)).ToArray()), aps);
      byte[] hashedResponse1 = null;
      using (var prov = new MD5CryptoServiceProvider()) hashedResponse1 = prov.ComputeHash(Encoding.UTF8.GetBytes(responseString1));
      var builder = new StringBuilder();
      foreach (var item in hashedResponse1) builder.Append(item.ToString("x2"));
      var responseString2 = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}&sig={1}", string.Join("&", response.Select(p => string.Format("{0}={1}", p.Key, p.Value)).ToArray()), builder.ToString().ToLower()))); ;
      // Write for the second time
      writer.Write(string.Format("<response xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">{0}</response>", responseString2));
      writer.Flush();
      Thread.Sleep(500);
      MessageBox.Show(reader.ReadToEnd());
    }
  }

I shortened and shrunk the code as much as possible, because I think my SASL implementation (whether it works or not, I haven’t had a chance to test it yet) is not what causes the error.

I get the following exception thrown at my face: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine.
10053
System.Net.Sockets.SocketError.ConnectionAborted

It happens every time I try to read from client‘s stream for the second time. As you can see i pause a thread here so Facebook server has enough time to answer me, but I used asynchronous approach before and I encountered the exact same thing, so I decided to try it synchronously first. Anyway actual SASL mechanism implementation really shouldn’t cause this because if I don’t try to authenticate right away, but I send the request to see what mechanisms server uses and select that mechanism in another round of reading and writing, it fails, but when I send mechanism selection XML right away, it works and fails on whatever second I send.

So the conclusion is following: I open the socket connection, write to it, read from it (first read works both sync and async), write to it for the second time and try to read from it for the second time and here it always fails. Clearly then, problem is with socket connection itself. I tried to use new StreamReader for second read but to no avail. This is rather unpleasant since I would really like to implement facade over NetworkStream with “Received” event or something like Send(string data, Action<string> responseProcessor) to get some comfort working with that stream, and I already had the implementation, but it also failed on second read.

Thanks for your suggestions.

Edit: Here is the code of facade over NetworkStream. Same thing happens when using this asynchronous approach, but couple of hours ago it worked, but for second response returned same string as for first. I can’t figute out what I changed in a meantime and how.

public void Send(XElement fragment)
{
  if (Sent != null) Sent(this, new XmppEventArgs(fragment));
  byte[] buffer = new byte[1024];
  AsyncCallback callback = null;
  callback = (a) => 
  {
    var available = NetworkStream.EndRead(a);
    if (available > 0)
    {
      StringBuilder.Append(Encoding.UTF8.GetString(buffer, 0, available));
      NetworkStream.BeginRead(buffer, 0, buffer.Length, callback, buffer);
    }
    else
    {
      var args = new XmppEventArgs(XElement.Parse(StringBuilder.ToString()));
      if (Received != null) Received(this, args);
      StringBuilder = new StringBuilder();
      // NetworkStream.BeginRead(buffer, 0, buffer.Length, callback, buffer);
    }
  };
  NetworkStream.BeginRead(buffer, 0, buffer.Length, callback, buffer);
  NetworkStreamWriter.Write(fragment);
  NetworkStreamWriter.Flush();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:45:03+00:00Added an answer on June 9, 2026 at 10:45 am

    The reader.ReadToEnd() call consumes everything until end-of-stream, i.e. until TCP connection is closed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement the registration plugin on my application as documented here: http://developers.facebook.com/docs/plugins/registration/advanced/
I'm trying to implement a simple Facebook chat application on Android. It lets the
I've been trying to implement the Facebook SDK into my application in order to
I'm trying to implement Facebook Realtime api with my application. I want to pull
Trying to implement the example from here. Facebook Share passes along the URL of
I was trying to implement Facebook login in windows 8 application . I am
I am trying to implement facebook Connect to my cakephp Application. i am using
im trying to implement a login with facebook/twitter functionality in my app, i read
I'm trying hard to find a way to implement the Facebook api in cocos
Trying to implement a search similar to here .This searches properties based on city,locality,property

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.