I'am trying to write simple library for facebook chat, but I've encountered an issue at the very beginning. I do not know why chat.facebook.com sends me "<" after sending auth element. Having searched through the Internet I found nothing that colud help me. Related code is at a very end of following code block.
public const string BEGIN_AUTH_PLAIN = @"<auth xmlns='urn:ietf:params:xml:ns:xmpp- sasl' mechanism='PLAIN'>";
public const string END_AUTH = @"</auth>";
public static string GetPlainAuth(string email, string password)
{
byte[] plain = System.Text.Encoding.UTF8.GetBytes((char)0+email+(char)0+password);
string credentials = System.Convert.ToBase64String(plain);
return String.Format("{0}{1}{2}", BEGIN_AUTH_PLAIN, credentials, END_AUTH);
}
//////////////////////////////////////////////////////////////////////////////
public async Task<bool> NegotiateStream()
{
if (!connected)
return false;
writer.WriteString(XmlConstants.BEGIN_STREAM);
await writer.StoreAsync();
await reader.LoadAsync(4096);
string resp = reader.ReadString(reader.UnconsumedBufferLength);
//check if response is stream and offers plaintext mechanism
if (!resp.Contains("stream:stream") || !resp.Contains("<mechanism>PLAIN"))
return false;
writer.WriteString(XmlConstants.START_TLS);
await writer.StoreAsync();
await reader.LoadAsync(4096);
resp = reader.ReadString(reader.UnconsumedBufferLength);
if (!resp.Contains("proceed"))
return false;
//now upgrate to tls connection
await socket.UpgradeToSslAsync(SocketProtectionLevel.Ssl, HostName);
//now try to login
writer.WriteString(XmlConstants.BEGIN_STREAM);
await writer.StoreAsync();
await reader.LoadAsync(4096);
resp = reader.ReadString(reader.UnconsumedBufferLength);
if (!resp.Contains("stream:stream") || !resp.Contains("<mechanism>PLAIN"))
return false;
//now send auth
string xx = XmlConstants.GetPlainAuth(Email, Password);
writer.WriteString(xx);
await writer.StoreAsync();
await reader.LoadAsync(4096);
resp = reader.ReadString(reader.UnconsumedBufferLength);
//could not login
if (!resp.Contains("success"))
return false;
return true;
}