AgentsState IAgent.IsAuthorized(ISheduler sheduler, ShedulerState state)
        {
            var error = Authorize(state.Reader, state.Content, AuthenticationKind.User);

            var response = sheduler.GetCommand(state, AuthSchemes.Digest, GetAuthorizationError(error));

            if (response.Command == AuthorizationCommands.TryAgain)
            {
                response.Writer.WriteXErrorDetails(GetError(error));

                var realm = GetRealm(state.Reader);
                if (realm.IsValid)
                {
                    WriteUnauthenticate(false, response.Writer, IsNonceStale(error), realm);
                }
                else
                {
                    response.Writer.WriteXErrorDetails(GetError(ErrorCodes.InvalidDomain));
                }
            }
            else if (response.Command == AuthorizationCommands.Cancel)
            {
                response.Writer.WriteXErrorDetails(GetError(error));
            }

            return(new AgentsState(response));
        }
        AgentsState IAgent.IsAuthorized(ISheduler sheduler, ShedulerState state)
        {
            int  opaque;
            bool proxy;
            ArraySegment <byte> token;
            var error = Authorize(state.Reader, scheme, out token, out opaque, out proxy);

            AuthorizationError error1;

            switch (error)
            {
            case ErrorCodes.Ok:
                error1 = AuthorizationError.Success;
                break;

            case ErrorCodes.Continue:
                error1 = AuthorizationError.Continue;
                break;

            case ErrorCodes.NoResponse:
                error1 = AuthorizationError.None;
                break;

            default:
                error1 = AuthorizationError.Failed;
                break;
            }

            var response   = sheduler.GetCommand(state, scheme, error1);
            var targetname = state.Reader.RequestUri.Hostport.Host;

            if (response.Command == AuthorizationCommands.TryAgain)
            {
                response.Writer.WriteAuthenticateMs(proxy, scheme, targetname, realm, opaque);
                response.Writer.WriteXErrorDetails(GetError(error));
                response.Writer.WriteDate(DateTime.UtcNow);
            }
            else if (response.Command == AuthorizationCommands.Continue)
            {
                response.Writer.WriteAuthenticateMs(proxy, scheme, targetname, realm, opaque, token);
                response.Writer.WriteDate(DateTime.UtcNow);
            }
            else if (response.Command == AuthorizationCommands.Cancel)
            {
                response.Writer.WriteXErrorDetails(GetError(error));
            }

            return(new AgentsState(response));
        }
        AgentsState IAgent.IsAuthorized(ISheduler sheduler, ShedulerState state)
        {
            var error = IsAuthorizedInternal(sheduler, state, AuthenticationKind.User);

            var response = sheduler.GetCommand(state, AuthSchemes.Digest, GetAuthorizationError(error));

            if (response.Command == AuthorizationCommands.TryAgain)
            {
                response.Writer.WriteXErrorDetails(GetError(error));
                WriteAuthenticateDigest(false, response.Writer, IsNonceStale(error), state.Realm);
            }
            else if (response.Command == AuthorizationCommands.Cancel)
            {
                response.Writer.WriteXErrorDetails(GetError(error));
            }

            return(new AuthorizationAgentsState <HttpMessageWriter>(response));
        }