private async Task RedirectToErrorPageAsync(HttpContext context)
    {
        var errorModel = new ErrorMessage
        {
            RequestId        = context.TraceIdentifier,
            Error            = Response.Error,
            ErrorDescription = Response.ErrorDescription,
            UiLocales        = Response.Request?.UiLocales,
            DisplayMode      = Response.Request?.DisplayMode,
            ClientId         = Response.Request?.ClientId
        };

        if (Response.RedirectUri != null && Response.Request?.ResponseMode != null)
        {
            // if we have a valid redirect uri, then include it to the error page
            errorModel.RedirectUri  = BuildRedirectUri();
            errorModel.ResponseMode = Response.Request.ResponseMode;
        }

        var message = new Message <ErrorMessage>(errorModel, _clock.UtcNow.UtcDateTime);
        var id      = await _errorMessageStore.WriteAsync(message);

        var errorUrl = _options.UserInteraction.ErrorUrl;

        var url = errorUrl.AddQueryString(_options.UserInteraction.ErrorIdParameter, id);

        context.Response.Redirect(_urls.GetAbsoluteUrl(url));
    }
    /// <summary>
    /// Executes the result.
    /// </summary>
    /// <param name="context">The HTTP context.</param>
    /// <returns></returns>
    public async Task ExecuteAsync(HttpContext context)
    {
        Init(context);

        var returnUrl = _urls.BasePath.EnsureTrailingSlash() + Constants.ProtocolRoutePaths.AuthorizeCallback;

        if (_authorizationParametersMessageStore != null)
        {
            var msg = new Message <IDictionary <string, string[]> >(_request.ToOptimizedFullDictionary());
            var id  = await _authorizationParametersMessageStore.WriteAsync(msg);

            returnUrl = returnUrl.AddQueryString(Constants.AuthorizationParamsStore.MessageStoreIdParameterName, id);
        }
        else
        {
            returnUrl = returnUrl.AddQueryString(_request.ToOptimizedQueryString());
        }

        if (!_url.IsLocalUrl())
        {
            // this converts the relative redirect path to an absolute one if we're
            // redirecting to a different server
            returnUrl = _urls.Origin + returnUrl;
        }

        var url = _url.AddQueryString(_options.UserInteraction.CustomRedirectReturnUrlParameter, returnUrl);

        context.Response.Redirect(_urls.GetAbsoluteUrl(url));
    }