Пример #1
0
        public virtual void Bind(SaslRequest saslRequest)
        {
            if (saslRequest == null)
            {
                throw new ArgumentNullException(nameof(saslRequest));
            }

            Hashtable saslBindProperties = null;

            using (var saslClient = CreateClient(saslRequest.SaslMechanism, saslRequest.AuthorizationId,
                                                 DefaultSaslClientFactory.ProtocolLdap, Host,
                                                 saslRequest.Credentials, saslBindProperties))
            {
                if (saslClient == null)
                {
                    throw new ArgumentException("Unsupported Sasl Authentication mechanism: " + saslRequest.SaslMechanism);
                }

                var constraints = saslRequest.Constraints ?? _defSearchCons;

                try
                {
                    var bindProps = new BindProperties(LdapV3, saslRequest.AuthorizationId, "sasl", anonymous: false, bindProperties: saslBindProperties);
                    var bindSemId = Connection.AcquireWriteSemaphore();
                    Connection.SetBindSemId(bindSemId);

                    byte[] clientResponse = null;
                    if (saslClient.HasInitialResponse)
                    {
                        clientResponse = saslClient.EvaluateChallenge(Array.Empty <byte>());
                    }

                    while (!saslClient.IsComplete)
                    {
                        try
                        {
                            var replyBuf = SendLdapSaslBindRequest(clientResponse, saslClient.MechanismName, bindProps, constraints);

                            if (replyBuf != null)
                            {
                                clientResponse = saslClient.EvaluateChallenge(replyBuf);
                            }
                            else
                            {
                                clientResponse = saslClient.EvaluateChallenge(Array.Empty <byte>());
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new LdapException("Unexpected SASL error.", LdapException.Other, null, ex);
                        }
                    }
                }
                catch (Exception e)
                {
                    throw new LdapException(e);
                }
            }
        }