Jabber.NET SASL异常/验证错误

时间:2012-10-22 08:16:49

标签: c# .net xmpp openfire

我使用 Jabber.NET 连接到 OpenFire 服务器。客户端给我一个奇怪的异常有时(但大部分时间):jabber.connection.sasl.SASLException: Hash ist im angegebenen Status nicht gültig,这是一条德语消息,表明散列对指定的状态无效。 有时候(是的,我知道,但我无法影响)它,我也得到了异常jabber.connection.sasl.SASLException: Das SafeHandle wurde geschlossen,这意味着SafeHandle已被关闭。

我尝试关闭所有可能的安全设置,但这一切都没有效果。这就是我的联系方式:

    private JabberClient _jabberClient = null;
    public JabberClient XmppClient
    {
        get
        {
            if (_jabberClient == null)
                _jabberClient = new JabberClient();
            return _jabberClient;
        }
    }

    // double checked: username and password are correct, servername is existing URL
    public login(username,password,servername)
    {
        var jid = new JID(username, serverName, Guid.NewGuid().ToString());
        bedrock.net.AsyncSocket.UntrustedRootOK = true;
        XmppClient.RequiresSASL = false; // --> this seems to have no effect at all
        //XmppClient[Options.SASL_MECHANISMS] = MechanismType.PLAIN; // setting this throws a NotImplementedException!!!
        XmppClient.SSL = false;
        XmppClient.AutoPresence = true;
        XmppClient.AutoReconnect = 3.0f;
        XmppClient.AutoLogin = true;
        XmppClient.AutoStartTLS = true;
        XmppClient.AutoStartCompression = false;
        XmppClient.User = jid.User;
        XmppClient.Password = password;
        XmppClient.Resource = jid.Resource;
        XmppClient.Server = serverName;
        XmppClient.NetworkHost = serverName;

        XmppClient.OnMessage += new MessageHandler(JabberClient_OnMessage);
        XmppClient.OnDisconnect += new bedrock.ObjectHandler(JabberClient_OnDisconnect);
        XmppClient.OnError += new bedrock.ExceptionHandler(JabberClient_OnError);
        XmppClient.OnAuthError += new jabber.protocol.ProtocolHandler(JabberClient_OnAuthError);
        XmppClient.OnAuthenticate += new bedrock.ObjectHandler(XmppClient_OnAuthenticate);
        XmppClient.OnInvalidCertificate += new System.Net.Security.RemoteCertificateValidationCallback(XmppClient_OnInvalidCertificate);
        XmppClient.OnWriteText += new bedrock.TextHandler(XmppClient_OnWriteText);
    }

我该如何解决这个问题?什么可能绕过它?我现在唯一可能的做法是多次验证,直到它幸运地工作,但这可能需要一些时间,我不想诉诸于此。

编辑:下面的答案给了我答案:它就像识别异常并再次调用connect()一样简单。为什么库不会自动执行此操作对我来说是个谜,我也不明白为什么它在停用时使用SASL,但是很好。被黑了,有效。谢谢!

1 个答案:

答案 0 :(得分:0)

我没有看到问题,但这是我的工作代码。希望它有所帮助。

    private JabberClient _client;
    private JabberClient client
    {
        get {
            if (_client == null)
            {
                _client = new JabberClient();
                ConfigureClient(_client);
            }
            return _client; 
        }
        set
        {
            _client = value;
            RegisterOnEvents(_client);
        }
    }      

    public MiddleMan()
    {
        jabber.connection.ConferenceManager c = new jabber.connection.ConferenceManager();

        client = new JabberClient();

        ConfigureClient(client);
    }  

    private void ConfigureClient(JabberClient jc)
    {
        JID jid = new JID(LoginInformation.UserName);

        jc.User = jid.User;
        jc.Server = jid.Server;
        if (!String.IsNullOrEmpty(LoginInformation.NetworkHost))
        {
            jc.NetworkHost = LoginInformation.NetworkHost;
        }
        jc.Port = 5222;
        jc.AutoReconnect = 3f;
        jc.AutoLogin = true;
        jc.AutoPresence = true;
        jc.Resource = LoginInformation.Resource;
        jc.Password = LoginInformation.Password;
        jc.KeepAlive = 10;
    }

    private void RegisterOnEvents(JabberClient jc)
    {
        //Messaging
        //jc.OnMessage += new MessageHandler(OnMessageReceived);

        //Login
        jc.OnLoginRequired += (sender) =>
        {
           //code
        };

        jc.OnAuthenticate += (sender) =>
        {
            //code
        };

        jc.OnAuthError += (sender, rp) =>
        {
            //code
            this.Connect();
        };

        //Connection
        jc.OnConnect += (sender, stream) =>
        {
            //code
        };

        jc.OnDisconnect += (sender) =>
        {
            //code
            this.Connect();
        };

        jc.OnError += (sender, ex) =>
        {
            //code
            this.Connect();
        };

        //jc.OnPresence += new PresenceHandler(OnPresence);
    }