public async static Task <TResult> AuthenticationAsync <TResult>(
            EastFive.Azure.Auth.Method authentication, IDictionary <string, string> values,
            IAzureApplication application, IHttpRequest request,
            IInvokeApplication endpoints, Uri baseUri,
            IRefOptional <Authorization> authorizationRefToCreate,
            Func <Uri, Guid?, Func <IHttpResponse, IHttpResponse>, TResult> onRedirect,
            Func <TResult> onAuthorizationNotFound,
            Func <string, TResult> onCouldNotConnect,
            Func <string, TResult> onGeneralFailure)
        {
            var telemetry = application.Telemetry;

            return(await await authentication.RedeemTokenAsync(values, application,
                                                               async (externalAccountKey, authorizationRefMaybe, loginProvider, extraParams) =>
            {
                #region Handle case where there was a direct link or a POST

                if (!authorizationRefMaybe.HasValue)
                {
                    var authorization = new Authorization
                    {
                        authorizationRef = authorizationRefToCreate.HasValue?
                                           authorizationRefToCreate.Ref
                                :
                                           new Ref <Authorization>(Security.SecureGuid.Generate()),
                        Method = authentication.authenticationId,
                        parameters = extraParams,
                        authorized = true,
                    };
                    return await ProcessAsync(authorization,
                                              async(authorizationUpdated) =>
                    {
                        bool created = await authorizationUpdated.StorageCreateAsync(
                            authIdDiscard =>
                        {
                            return true;
                        },
                            () =>
                        {
                            //if(authorizationRefToCreate.HasValueNotNull())
                            //    throw new Exception("Authorization to create already exists.");
                            //throw new Exception("Duplicated update from ProcessAsync.");
                            return false;
                        });
                    },
                                              authentication, externalAccountKey, extraParams,
                                              application, request, endpoints, loginProvider, baseUri,
                                              onRedirect,
                                              onGeneralFailure,
                                              telemetry);
                }

                #endregion

                var authorizationRef = authorizationRefMaybe.Ref;
                return await authorizationRef.StorageUpdateAsync(
                    async(authorization, saveAsync) =>
                {
                    return await ProcessAsync(authorization,
                                              saveAsync,
                                              authentication, externalAccountKey, extraParams,
                                              application, request, endpoints, loginProvider, baseUri,
                                              onRedirect,
                                              onGeneralFailure,
                                              telemetry);
                },
                    () =>
                {
                    return onAuthorizationNotFound();
                });
            },
                                                               (authorizationRef, extraparams) => onGeneralFailure("Cannot use logout to authenticate").AsTask(),
                                                               (why) => onCouldNotConnect(why).AsTask(),
                                                               (why) => onGeneralFailure(why).AsTask()));
        }
        private async static Task <TResult> AuthenticationAsync <TResult>(Guid requestId,
                                                                          EastFive.Azure.Auth.Method authentication, IDictionary <string, string> values, Uri baseUri,
                                                                          AzureApplication application,
                                                                          Func <Uri, TResult> onRedirect,
                                                                          Func <TResult> onAuthorizationNotFound,
                                                                          Func <string, TResult> onCouldNotConnect,
                                                                          Func <string, TResult> onGeneralFailure)
        {
            var authorizationRequestManager = application.AuthorizationRequestManager;
            var telemetry = application.Telemetry;

            return(await await authentication.RedeemTokenAsync(values, application,
                                                               async (externalAccountKey, authorizationRefMaybe, loginProvider, extraParams) =>
            {
                #region Handle case where there was a direct link

                if (!authorizationRefMaybe.HasValue)
                {
                    var authorization = new Authorization
                    {
                        authorizationRef = new Ref <Authorization>(Security.SecureGuid.Generate()),
                        Method = authentication.authenticationId,
                        parameters = extraParams,
                        authorized = true,
                    };
                    return await ProcessAsync(authorization,
                                              async(authorizationUpdated) =>
                    {
                        bool created = await authorizationUpdated.StorageCreateAsync(
                            authIdDiscard =>
                        {
                            return true;
                        },
                            () => throw new Exception("Secure guid not unique"));
                    },
                                              authentication, externalAccountKey, extraParams,
                                              requestId, baseUri, application, loginProvider,
                                              onRedirect,
                                              onGeneralFailure,
                                              telemetry);
                }

                #endregion

                var authorizationRef = authorizationRefMaybe.Ref;
                return await authorizationRef.StorageUpdateAsync(
                    async(authorization, saveAsync) =>
                {
                    return await ProcessAsync(authorization,
                                              saveAsync,
                                              authentication, externalAccountKey, extraParams,
                                              requestId, baseUri, application, loginProvider,
                                              onRedirect,
                                              onGeneralFailure,
                                              telemetry);
                },
                    () =>
                {
                    return onAuthorizationNotFound();
                });
            },
                                                               (authorizationRef, extraparams) => onGeneralFailure("Cannot use logout to authenticate").AsTask(),
                                                               (why) => onCouldNotConnect(why).AsTask(),
                                                               (why) => onGeneralFailure(why).AsTask()));
        }