public static async Task <IHttpResponse> ReplayAsync(
            [QueryId(Name = AuthorizationIdPropertyName)] IRef <Authorization> authorizationRef,
            Api.Azure.AzureApplication application,
            IInvokeApplication endpoints,
            IHttpRequest request,
            ContentTypeResponse <Session> onReplayed,
            NotFoundResponse onNotFound,
            ForbiddenResponse onAuthorizationFailed,
            ServiceUnavailableResponse onServericeUnavailable,
            ForbiddenResponse onInvalidMethod,
            GeneralConflictResponse onFailure)
        {
            return(await await authorizationRef.StorageGetAsync(
                       async (authorization) =>
            {
                var methodRef = authorization.Method;
                return await await Auth.Method.ById(methodRef, application,
                                                    async(method) =>
                {
                    var paramsUpdated = authorization.parameters
                                        .Append(authorizationRef.id.ToString().PairWithKey("state"))
                                        .ToDictionary();

                    //var authorizationRequestManager = application.AuthorizationRequestManager;
                    return await await Redirection.AuthenticationAsync(
                        method,
                        paramsUpdated,
                        application, request,
                        endpoints,
                        request.RequestUri,
                        authorizationRef.Optional(),
                        async(redirect, accountIdMaybe, modifier) =>
                    {
                        var sessionRef = Ref <Session> .SecureRef();
                        var session = new Session()
                        {
                            sessionId = sessionRef,
                            account = accountIdMaybe,
                            authorization = authorizationRef.Optional(),
                        };
                        var responseCreated = await Session.CreateAsync(sessionRef, authorizationRef.Optional(),
                                                                        session,
                                                                        application,
                                                                        (sessionCreated, contentType) =>
                        {
                            var response = onReplayed(sessionCreated, contentType: contentType);
                            response.SetLocation(redirect);
                            return response;
                        },
                                                                        onAlreadyExists: default,
        public static async Task <TResult> ProcessRequestAsync <TResult>(
            EastFive.Azure.Auth.Method method,
            IDictionary <string, string> values,
            AzureApplication application, HttpRequestMessage request,
            System.Web.Http.Routing.UrlHelper urlHelper,
            Func <Uri, TResult> onRedirect,
            Func <string, TResult> onBadCredentials,
            Func <string, TResult> onCouldNotConnect,
            Func <string, TResult> onFailure)
        {
            var authorizationRequestManager = application.AuthorizationRequestManager;

            var telemetry = application.Telemetry;

            telemetry.TrackEvent($"ResponseController.ProcessRequestAsync - Requesting credential manager.");

            var requestId   = Guid.NewGuid();
            var redirection = new Redirection
            {
                webDataRef     = requestId.AsRef <Redirection>(),
                method         = method.authenticationId,
                values         = values,
                redirectedFrom = request.Headers.Referrer,
            };

            return(await await redirection.StorageCreateAsync(
                       discard =>
            {
                var baseUri = request.RequestUri;
                return AuthenticationAsync(requestId,
                                           method, values, baseUri, application,
                                           onRedirect,
                                           () => onFailure("Authorization not found"),
                                           onCouldNotConnect,
                                           onFailure);
            },
                       () => onFailure("GUID NOT UNIQUE").AsTask()));
        }
        public async static Task <IHttpResponse> CreateAuthorizedAsync(
            [UpdateId(Name = AuthorizationIdPropertyName)] IRef <Authorization> authorizationRef,
            [Property(Name = MethodPropertyName)] IRef <Method> methodRef,
            [Property(Name = ParametersPropertyName)] IDictionary <string, string> parameters,
            [Resource] Authorization authorization,
            Api.Azure.AzureApplication application, IProvideUrl urlHelper,
            IInvokeApplication endpoints,
            IHttpRequest request,
            CreatedResponse onCreated,
            AlreadyExistsResponse onAlreadyExists,
            ForbiddenResponse onAuthorizationFailed,
            ServiceUnavailableResponse onServericeUnavailable,
            ForbiddenResponse onInvalidMethod)
        {
            authorization.accountIdMaybe = default;
            authorization.authorized     = false;
            return(await await Auth.Method.ById(methodRef, application,
                                                (method) =>
            {
                var paramsUpdated = parameters;
                //.Append(
                //    authorizationRef.id.ToString().PairWithKey("state"))
                //.ToDictionary();

                return Redirection.AuthenticationAsync(
                    method,
                    paramsUpdated,
                    application, request, endpoints, request.RequestUri,
                    authorizationRef.Optional(),
                    (redirect, accountIdMaybe, discardModifier) => onCreated(),
                    () => onAuthorizationFailed().AddReason("Authorization was not found"),     // Bad credentials
                    why => onServericeUnavailable().AddReason(why),
                    why => onAuthorizationFailed().AddReason(why));
            },
                                                () => onInvalidMethod().AddReason("The method was not found.").AsTask()));
        }
        public static async Task <IHttpResponse> ProcessRequestAsync(
            EastFive.Azure.Auth.Method method,
            IDictionary <string, string> values,
            IAzureApplication application,
            IHttpRequest request,
            IInvokeApplication endpoints,
            IProvideUrl urlHelper,
            Func <Uri, Guid?, IHttpResponse> onRedirect,
            Func <string, IHttpResponse> onAuthorizationnotFound,
            Func <string, IHttpResponse> onCouldNotConnect,
            Func <string, IHttpResponse> onFailure)
        {
            //var authorizationRequestManager = application.AuthorizationRequestManager;

            var telemetry = application.Telemetry;

            telemetry.TrackEvent($"ResponseController.ProcessRequestAsync - Requesting credential manager.");

            var requestId = Guid.NewGuid();

            request.TryGetReferer(out Uri referer);
            var redirection = new Redirection
            {
                webDataRef     = requestId.AsRef <Redirection>(),
                method         = method.authenticationId,
                values         = values,
                redirectedFrom = referer,
            };

            return(await await redirection.StorageCreateAsync(
                       discard =>
            {
                return EastFive.Azure.AppSettings.Auth.PauseRedirections.ConfigurationBoolean(
                    async pauseRedirections =>
                {
                    if (pauseRedirections)
                    {
                        return request.CreateResponse(System.Net.HttpStatusCode.OK, $"All logins have been paused for this system.  Request ID `{requestId}`");
                    }
                    return await ContinueAsync();
                },
                    why => ContinueAsync(),
                    ContinueAsync);

                Task <IHttpResponse> ContinueAsync()
                {
                    var baseUri = request.RequestUri;
                    return AuthenticationAsync(
                        method, values, application, request, endpoints,
                        request.RequestUri,
                        RefOptional <Authorization> .Empty(),
                        (uri, accountIdMaybe, modifier) =>
                    {
                        var response = onRedirect(uri, accountIdMaybe);
                        return modifier(response);
                    },
                        () => onAuthorizationnotFound("Authorization not found"),
                        onCouldNotConnect,
                        onFailure);
                }
            },
                       () => onFailure("GUID NOT UNIQUE").AsTask()));
        }