public bool Authenticate(ICredentialsProvider provider)
        {
            log.Info("Authenticating");
            if (provider == null)
            {
                throw new ArgumentNullException("AuthenticationInfo can not be null in order to authenticate.");
            }
            AuthenticationInfo authInfoToUse = null;
            try
            {
                authInfoToUse = provider.GetCredentials();
                if (authInfoToUse == null)
                    throw new InvalidCredentialsException("Credential provider returned null");
            }
            catch (InvalidCredentialsException ice)
            {
                log.Error("Failed to obtain credentials.", ice);
                return false;
            }

            // save important information
            lock (this)
            {
                this.provider = provider;
                this.clientAuthInfo = authInfoToUse;
                this.usingAuth = true;
            }

            // build NetMessage

            string actionId = System.Guid.NewGuid().ToString();

            NetAuthentication netAuth = new NetAuthentication(authInfoToUse.Token, authInfoToUse.UserAuthenticationType);
            if ((authInfoToUse.Roles != null) && (authInfoToUse.Roles.Count != 0))
                netAuth.Roles = authInfoToUse.Roles;
            if (authInfoToUse.UserId != null)
            {
                netAuth.UserId = authInfoToUse.UserId;
            }
            netAuth.ActionId = actionId;

            NetAction netAction = new NetAction(NetAction.ActionType.AUTH);
            netAction.AuthenticationMessage = netAuth;
            NetMessage msg = new NetMessage(netAction);

            // build waitable object
            WaitMessageAccepted waitMsgAccepted = new WaitMessageAccepted();
            AcceptRequest acceptRequest = new AcceptRequest(actionId, waitMsgAccepted, 7000);

            //send message
            HandleOutgoingMessage(msg, acceptRequest);

            // wait for response
            lock (waitMsgAccepted.SyncObject)
            {
                Monitor.Wait(waitMsgAccepted.SyncObject);
            }
            if (waitMsgAccepted.WaitResult != WaitMessageAccepted.Result.Accepted)
            {
                log.Error("Authenticatation failed. Reason: " + waitMsgAccepted.WaitResult);
                return false;
            }

            log.Info("Authenticated");

            return true;
        }
        public bool Authenticate(ICredentialsProvider provider)
        {
            log.Info("Authenticating");
            if (provider == null)
            {
                throw new ArgumentNullException("AuthenticationInfo can not be null in order to authenticate.");
            }
            AuthenticationInfo authInfoToUse = null;

            try
            {
                authInfoToUse = provider.GetCredentials();
                if (authInfoToUse == null)
                {
                    throw new InvalidCredentialsException("Credential provider returned null");
                }
            }
            catch (InvalidCredentialsException ice)
            {
                log.Error("Failed to obtain credentials.", ice);
                return(false);
            }

            // save important information
            lock (this)
            {
                this.provider       = provider;
                this.clientAuthInfo = authInfoToUse;
                this.usingAuth      = true;
            }

            // build NetMessage

            string actionId = System.Guid.NewGuid().ToString();

            NetAuthentication netAuth = new NetAuthentication(authInfoToUse.Token, authInfoToUse.UserAuthenticationType);

            if ((authInfoToUse.Roles != null) && (authInfoToUse.Roles.Count != 0))
            {
                netAuth.Roles = authInfoToUse.Roles;
            }
            if (authInfoToUse.UserId != null)
            {
                netAuth.UserId = authInfoToUse.UserId;
            }
            netAuth.ActionId = actionId;

            NetAction netAction = new NetAction(NetAction.ActionType.AUTH);

            netAction.AuthenticationMessage = netAuth;
            NetMessage msg = new NetMessage(netAction);

            // build waitable object
            WaitMessageAccepted waitMsgAccepted = new WaitMessageAccepted();
            AcceptRequest       acceptRequest   = new AcceptRequest(actionId, waitMsgAccepted, 7000);

            //send message
            HandleOutgoingMessage(msg, acceptRequest);

            // wait for response
            lock (waitMsgAccepted.SyncObject)
            {
                Monitor.Wait(waitMsgAccepted.SyncObject);
            }
            if (waitMsgAccepted.WaitResult != WaitMessageAccepted.Result.Accepted)
            {
                log.Error("Authenticatation failed. Reason: " + waitMsgAccepted.WaitResult);
                return(false);
            }

            log.Info("Authenticated");

            return(true);
        }