简体   繁体   中英

How to create a new user using jabber net in XMPP server using C#?

I got stuck with creating/registering a new user using jabber.net library in C#. I am using a ejabberd as server. here is my code:

 private void btnCreateAccount_Click(object sender, EventArgs e)
    {
        IQ iq1 = new IQ(jabberClient1.Document);
        iq1.ID = "reg1";
        iq1.Type = IQType.set;
        iq1.InnerXml = "<query xmlns='jabber:iq:register'></query>";
        jabberClient1.Tracker.BeginIQ(iq1, new jabber.connection.IqCB(on_create_user), null);


        IQ iq = new IQ(jabberClient1.Document);
        iq.ID = "reg2";
        iq.Type = IQType.set;
        iq.InnerXml = "<query xmlns='jabber:iq:register'><username>hax0r</username><password>god</password></query>";
        jabberClient1.Tracker.BeginIQ(iq, new jabber.connection.IqCB(on_create_user), null);
    }

    private void on_create_user(object sender, IQ iq, object data)
    {
        Error err = iq.Error;
        if (err != null)
            Console.WriteLine("Error " + err.Condition + " on querying user " + data);
    }

When I run the above code, C# throws an error:

Exception:Thrown: "Object reference not set to an instance of an object." (System.NullReferenceException) A System.NullReferenceException was thrown: "Object reference not set to an instance of an object."

What am I doing wrong? Any help is highly appreciated.

I am still confused how to create a new user with jabber.net in C#. I have done the following but no success yet:

 JabberClient jc = new jabber.client.JabberClient(this.components);
 jc.AutoReconnect = 3F;
 jc.AutoStartCompression = true;
 jc.AutoStartTLS = true;
 jc.InvokeControl = this;
 jc.KeepAlive = 30F;
 jc.LocalCertificate = null;
 jc.Password = "username";
 jc.User = "password";
 jc.Server = "example.com";
 jc.Register(new JID(jc.User, jc.Server, null));

Any hints is highly appreciated.

Please check out the Example directory. MainForm.cs does exactly what you want. In particular, the Register call at line 764, then the callbacks for OnRegisterInfo and OnRegistered .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM