public partial class Chat : Page { public TcpClient client = new TcpClient(); NetworkStream stream; private SslStream ssl; private string AppId { get; set; } public string AppSecret { get; set; } public string AppUrl { get; set; } public string UserId { get; set; } public string AccessToken { get; set; } private string _error = string.Empty;//global error string for watch debugging in VS. public const string FbServer = "chat.facebook.com"; private const string STREAM_XML = ""; private const string AUTH_XML = ""; private const string CLOSE_XML = ""; private const string RESOURCE_XML = "fb_xmpp_script"; private const string SESSION_XML = ""; private const string START_TLS = ""; protected void Page_Load(object sender, EventArgs e) this.AppId = "000000082000090";//TODO get from appsettings. //AddAdditionalPermissions("xmpp_login");//TODO handle xmpp_login persmission this.AppSecret = "d370c1bfec9be6d9accbdf0117f2c495"; //TODO Get appsecret from appsetting. this.AppUrl = "https://fbd.anteckna.nu"; SetUserNameAndAuthToken(); Connect(FbServer); // initiates auth process (using X-FACEBOOK_PLATFORM) InitiateAuthProcess(STREAM_XML); // starting tls - MANDATORY TO USE OAUTH TOKEN!!!! StartTlsConnection(START_TLS); // gets decoded challenge from server var decoded = GetDecodedChallenge(AUTH_XML); // creates the response and signature string response = CreateResponse(decoded); //send response to server SendResponseToServer(response); SendMessage("test"); // finishes auth process FinishAuthProcess(); // we made it! string streamresponseEnd = SendWihSsl(CLOSE_XML); } public void Connect(string server) { try { client.Connect(server, 5222); stream = client.GetStream(); } catch (ArgumentNullException e) { Console.WriteLine("ArgumentNullException: {0}", e); } catch (SocketException e) { Console.WriteLine("SocketException: {0}", e); } } private void InitiateAuthProcess(string streamXml) { string initiation = Send(streamXml); if (!initiation.Contains("X-FACEBOOK-PLATFORM")) _error = initiation; //TODO handle error } //Facebook answer //X-//FACEBOOK-PLATFORMPLAIN public string Send(string dataToSend) { Byte[] data = Encoding.UTF8.GetBytes(dataToSend); stream.Write(data, 0, data.Length); data = new Byte[2048]; Int32 bytes = stream.Read(data, 0, data.Length); string responseData = Encoding.UTF8.GetString(data, 0, bytes); return responseData; } private void StartTlsConnection(string startTls) { string tlsResult = Send(startTls); if (!tlsResult.ToLower().Contains("proceed")) _error = tlsResult; //TODO handle error ssl = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null); try { ssl.AuthenticateAsClient(FbServer, new X509CertificateCollection(), SslProtocols.Tls, false); } catch (AuthenticationException ex) { Console.WriteLine("Exception: {0}", ex.Message); if (ex.InnerException != null) { Console.WriteLine("Inner exception: {0}", ex.InnerException.Message); } Console.WriteLine("Authentication failed - closing the connection."); client.Close(); return; } string initiationSsl = SendWihSsl(STREAM_XML); if (!initiationSsl.Contains("X-FACEBOOK-PLATFORM")) _error = initiationSsl;//TODO handle error } //Response from facebook in tlsResult is: // //Response from Facebook in initiationSsl is : //X-FACEBOOK-PLATFORM //PLAIN public string SendWihSsl(string dataToSend) { Byte[] data = System.Text.Encoding.UTF8.GetBytes(dataToSend); ssl.Write(data, 0, data.Length); ssl.Flush(); data = new Byte[2048]; int myBytesRead = 0; StringBuilder myResponseAsSB = new StringBuilder(); do { myBytesRead = ssl.Read(data, 0, data.Length); myResponseAsSB.Append(System.Text.Encoding.UTF8.GetString(data, 0, myBytesRead)); if (myResponseAsSB.ToString().IndexOf("") + 2; int end = sslResponse.IndexOf("{0}", response); string response2 = SendWihSsl2(xml); if (!response2.ToLower().Contains("success")) _error = response2; } //After 100seconds I get :