The following walkthrough demonstrates the steps required to register an XMPP account through the in-band registration process.

The XMPP/Jabber protocols have long included a method for in-band registration with instant messaging servers and associated services. While not all XMPP servers support in-band registration, quite a few do and it's a very convenient way for a new user to register an account directly from within their chat client application.

In-Band Account Registration

  1. Create an instance of the XmppClient class using the constructor that does not expect username and password parameters, connect to the XMPP server on which you wish to register a new XMPP account and call the Register method, passing it a delegate of type RegistrationCallback.

    CopyC#
    using (XmppClient client = new XmppClient("jabber.ccc.de")) {
        // Setup any event handlers here, before connecting to the server.
        // In this example, we don't need to install any.
        client.Connect();
    
        // Try to initiate the in-band registration process with the server.
        client.Register(RegistrationCallback);
    
        [...]

  2. The RegistrationCallback is a method that is invoked during the registration process to let the user enter any data that the server requires in order to create a new account. Many servers merely require the user to enter their designated username and password, but some ask the user to provide additional information such as his or her real name or ask the the user to enter a CAPTCHA.

    To facilitate the different needs of server administrators, XMPP employs a flexible request/submit mechanism similar to HTML forms: The server sends a form with various fields and asks the client to fill it out. What kind of fields the form contains is entirely up to the server administrator and differs from server to server.

    A shortended version of the RegistrationCallback method is given below. You can find the full example code at the end of this walkthrough.

    CopyC#
    static SubmitForm RegistrationCallback(RequestForm form) {
        // The RequestForm contains all fields the user is required to fill out
        // in order to complete account registration with the XMPP server.
        if(!String.IsNullOrEmpty(form.Instructions))
            Console.WriteLine(form.Instructions);
    
        SubmitForm submitForm = new SubmitForm();
        // Go through each field of the form and gather the data from the user.
        foreach (var field in form.Fields) {
            // Gather the input data; XMPP dataforms support different field-types
            // allowing server administrators to flexibly gather whatever data
            // is required to register an account;
            DataField f = null;
    
            // The field is a text-field, similar to a WinForms TextControl or HTML input field.
            if (field is TextField)
                f = ReadTextField(field as TextField);
            // The field is a password field, similar to an HTML input field of type "password".
            else if (field is PasswordField)
                f = ReadPasswordField(field as PasswordField);
            // The field is a boolean field, which can be either true or false.
            else if (field is BooleanField)
                f = ReadBoolField(field as BooleanField);
            // The field is an input field expecting a Jabber ID (JID).
            else if (field is JidField)
                f = ReadJidField(field as JidField);
            // Hidden fields should just be returned to the server, usually without
            // modification. They serve the same purpose as hidden fields in HTML
            // forms.
            else if (field is HiddenField)
                f = field;
            else if (field is FixedField) {
                // Fixed fields are static labels of text that should be rendered to the user much
                // like a WinForms Label. They don't require any input.
                var fixedField = field as FixedField;
                Console.WriteLine("Static label: " + fixedField.Description);
            }
    
            if (f != null)
                submitForm.Fields.Add(f);
        }
        // Return the filled-out form to the server.
        return submitForm;
    }

  3. After the registration process has been completed, all that is left to do to use the newly created account, is to log in with the credentials that the user provided during the registration.

    CopyC#
    // Upon successful account registration, we can authenticate with the server.
    Console.Write("Enter your username: ");
    string username = Console.ReadLine();
    Console.Write("Enter your password: ");
    string password = Console.ReadLine();
    
    client.Authenticate(username, password);
    
    [...]

Example

You can view the full code for the above example here.

See Also