Code - Attach to Strophe Using C#

How to Attach Strophe With C#

I had to wire up a web chat application using Stophe.js without sending the password in the clear. Stophe has an attach function that you can use once you create the RID, SID and JID. To do that, you'll need to process everything using XEP 206.

In short, you'll be creating strings according to XEP-206, sending them to your jabber server and processing the responses. If you follow steps 3, 4 & 5, that will pretty much get you there. Let's get to it.

You may want to create a class to manage your web requests. Essentially everything is sent to the jabber server and the responses are returned as a XMLDocument. Create the strings similar to shown below, send to the jabber server, stuff the response into a XMLDocuemnt, evaluate the response looking for whatever XEP 206 shows, then proceed to the next step if successful.

if (webResponse.StatusCode == HttpStatusCode.OK) {
    using (responseStream = webResponse.GetResponseStream()){
        using (XmlTextReader xmlReader = new XmlTextReader(responseStream)){
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(xmlReader);
            xmlResponse = xmldoc;
            xmlReader.Close();
        }
        responseStream.Close();
    }
}
Step 1 - Get your user's Username and PW from your database (or wherever you store it) and create a RID by doing a random int. Make sure it has room to grow.

Step 2 - Create a request to get a SID and establish communications. Send your requests using HttpWebRequest to your jabber server url. I just used a StringBuilder to assemble my data to send. Once you should get a response that contains a SID and the AuthId. Save the SID. Check XEP 206 for more details on responses.

    sb.Append("<body content='text/xml; charset=utf-8'");
    sb.Append(" hold='1'");
    sb.Append(" rid='" + RID.ToString() + "'"); 
    sb.Append(" to='" + JabberServer + "'"); 
    sb.Append(" secure='true'");
    sb.Append(" wait='60'");
    sb.Append(" xml:lang='en'"); 
    sb.Append(" xmpp:version='1.0'");
    sb.Append(" xmlns='http://jabber.org/protocol/httpbind'");
    sb.Append(" xmlns:xmpp='urn:xmpp:xbosh'/>");
Step 3 - Authenticate. Essentially login your user in this step. Here you need to decide what your mechanism for encrypting is going to be, but for simplicity, we'll use PLAIN. You need to convert your username and password to a base64 or however you choose to send it. Here you start passing the SID back to the server. You MUST increment the RID each time. On this call, you'll be looking for a "success" response.

    var encoded= Convert.ToBase64String(Encoding.UTF8.GetBytes(x + [Username] + x + [Password]));
sb.Append("<body rid='" + RID + "'"); sb.Append(" sid='" + SID + "'"); sb.Append(" xmlns='http://jabber.org/protocol/httpbind'>"); sb.Append("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' "); sb.Append(" mechanism='PLAIN'>"); sb.Append(encoded); sb.Append("</auth>"); sb.Append("</body>");
Step 4 - Restart. Send a restart command back to the jabber server. This should return a stream features element if you are successful.

    sb.Append("<body rid='" + RID + "'"); 
    sb.Append(" xmlns='http://jabber.org/protocol/httpbind'");
    sb.Append(" sid='" + SID + "'");
    sb.Append(" to='" + JabberServer + "'");
    sb.Append(" xml:lang='en
    sb.Append(" xmpp:restart='true'"); 
    sb.Append(" xmlns:xmpp='urn:xmpp:xbosh'"); 
    sb.Append("/>");
Step 5 - Bind. This will bind your resource to the jabber id. The resource is what you use to represent where your user is (name of site, random crap - up to you). This is where you'll get the JID returned.

    sb.Append("<body rid='" + RID + "'");
    sb.Append(" sid='" + SID + "'");
    sb.Append(" xmlns='http://jabber.org/protocol/httpbind'>"); 
    sb.Append("<iq id='_bind_auth_2' type='set' xmlns='jabber:client'>");
    sb.Append("<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>");
    sb.Append("<resource>" + Resource + "</resource>");
    sb.Append("</bind>");
    sb.Append("</iq>");
    sb.Append("</body>"); 
Step 6 - Set the Session. This will establish the session on the jabber server with your shiny new JID. This returns a session id.

    sb.Append("<body rid='" + RID + "'");
    sb.Append(" sid='" + SID + "'");
    sb.Append(" xmlns='http://jabber.org/protocol/httpbind'>");
    sb.Append("<iq id='_session_auth_2' type='set' xmlns='jabber:client'>");
    sb.Append("<session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>");
    sb.Append("</iq>");
    sb.Append("</body>");
Overall, the process is pretty simple once you get it going. When you get though all the steps with success, pass the RID, SID and JID out to your web app. Use the Strophe attach function to get it going client side with the credentials you just created.
Once you're attached, that's another whole mess of fun. Don't forget to increment your RID each call then once more when you pass it back out to your web app.

var conn = new Strophe.Connection('http://jabberserver.url/http-bind/');
        conn.attach(JID, SID, RID, func);