/// <summary>
        /// Invokes the middleware.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="router">The router.</param>
        /// <returns></returns>
        public async Task Invoke(HttpContext context, IEndpointRouter router, IUserSession session)
        {
            // todo: review: right place to call this?
            await session.EnsureSessionIdCookieAsync();

            try
            {
                var endpoint = router.Find(context);
                if (endpoint != null)
                {
                    _logger.LogInformation("Invoking IdentityServer endpoint: {endpointType} for {url}", endpoint.GetType().FullName, context.Request.Path.ToString());

                    var result = await endpoint.ProcessAsync(context);

                    if (result != null)
                    {
                        _logger.LogTrace("Invoking result: {type}", result.GetType().FullName);
                        await result.ExecuteAsync(context);
                    }

                    return;
                }
            }
            catch (Exception ex)
            {
                await _events.RaiseAsync(new UnhandledExceptionEvent(ex));

                _logger.LogCritical("Unhandled exception: {exception}", ex.ToString());
                throw;
            }

            await _next(context);
        }
Exemple #2
0
        public async Task Invoke(HttpContext context, IEndpointRouter router)
        {
            try
            {
                var endpoint = router.Find(context);
                if (endpoint != null)
                {
                    _logger.LogInformation("Invoking WechatPay endpoint: {endpointType} for {url}", endpoint.GetType().FullName, context.Request.Path.ToString());

                    var result = endpoint.Process(context);

                    if (result != null)
                    {
                        _logger.LogTrace("Invoking result: {type}", result.GetType().FullName);
                        await result.ExecuteAsync(context);
                    }
                    return;
                }
                else
                {
                    context.Response.StatusCode = StatusCodes.Status404NotFound;
                }
            }
            catch (Exception ex)
            {
                _logger.LogCritical(ex, "Unhandled exception: {exception}", ex.Message);
            }
        }
Exemple #3
0
        public async Task Invoke(HttpContext context, IEndpointRouter router)
        {
            try
            {
                var endpoint = router.Find(context);
                if (endpoint != null)
                {
                    _logger.LogInformation("Invoking IdentityServer endpoint: {endpointType} for {url}", endpoint.GetType().FullName, context.Request.Path.ToString());

                    var result = await endpoint.ProcessAsync(context);

                    if (result != null)
                    {
                        _logger.LogTrace("Invoking result: {type}", result.GetType().FullName);
                        await result.ExecuteAsync(context);
                    }

                    return;
                }
            }
            catch (Exception ex)
            {
                _logger.LogCritical("Unhandled exception: {exception}", ex.ToString());
                throw;
            }

            await _next(context);
        }
        public async Task InvokeAsync(
            HttpContext context,
            RequestDelegate next)
        {
            try
            {
                var endpointHandler = _endpointRouter.Find(context);
                if (endpointHandler != null)
                {
                    _logger.LogInvokingEndpointHandler(
                        endpointHandler
                        .GetType()
                        .Name
                        ?? string.Empty,
                        context.Request.Path.ToString());
                    var result = await endpointHandler.ProcessAsync(context, context.RequestAborted);

                    _logger.LogInvokingEndpointResult(
                        result.GetType()
                        .Name
                        ?? string.Empty);
                    await result.ExecuteAsync(context, context.RequestAborted);

                    return;
                }
            }
            catch (Exception ex)
            {
                _logger.LogUnhandledException(ex);
                throw;
            }

            await next(context);
        }
        /// <summary>
        /// Invokes the middleware.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="router">The router.</param>
        /// <param name="session">The user session.</param>
        /// <param name="events">The event service.</param>
        /// <returns></returns>
        public async Task Invoke(HttpContext context, IEndpointRouter router, IUserSession session, IEventService events)
        {
            // this will check the authentication session and from it emit the check session
            // cookie needed from JS-based signout clients.
            await session.EnsureSessionIdCookieAsync();

            try
            {
                var endpoint = router.Find(context);
                if (endpoint != null)
                {
                    _logger.LogInformation("Invoking IdentityServer endpoint: {endpointType} for {url}", endpoint.GetType().FullName, context.Request.Path.ToString());

                    var result = await endpoint.ProcessAsync(context);

                    if (result != null)
                    {
                        _logger.LogTrace("Invoking result: {type}", result.GetType().FullName);
                        await result.ExecuteAsync(context);
                    }

                    return;
                }
            }
            catch (Exception ex)
            {
                await events.RaiseAsync(new UnhandledExceptionEvent(ex));

                _logger.LogCritical(ex, "Unhandled exception: {exception}", ex.Message);
                throw;
            }

            await _next(context);
        }
        /// <summary>
        /// Invokes the middleware.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="router">The router.</param>
        /// <param name="session">The user session.</param>
        /// <param name="events">The event service.</param>
        /// <param name="issuerNameService">The issuer name service</param>
        /// <param name="backChannelLogoutService"></param>
        /// <returns></returns>
        public async Task Invoke(
            HttpContext context,
            IEndpointRouter router,
            IUserSession session,
            IEventService events,
            IIssuerNameService issuerNameService,
            IBackChannelLogoutService backChannelLogoutService)
        {
            // this will check the authentication session and from it emit the check session
            // cookie needed from JS-based signout clients.
            await session.EnsureSessionIdCookieAsync();

            context.Response.OnStarting(async() =>
            {
                if (context.GetSignOutCalled())
                {
                    _logger.LogDebug("SignOutCalled set; processing post-signout session cleanup.");

                    // this clears our session id cookie so JS clients can detect the user has signed out
                    await session.RemoveSessionIdCookieAsync();

                    // back channel logout
                    var logoutContext = await session.GetLogoutNotificationContext();
                    if (logoutContext != null)
                    {
                        await backChannelLogoutService.SendLogoutNotificationsAsync(logoutContext);
                    }
                }
            });

            try
            {
                var endpoint = router.Find(context);
                if (endpoint != null)
                {
                    LicenseValidator.ValidateIssuer(await issuerNameService.GetCurrentAsync());

                    _logger.LogInformation("Invoking IdentityServer endpoint: {endpointType} for {url}", endpoint.GetType().FullName, context.Request.Path.ToString());

                    var result = await endpoint.ProcessAsync(context);

                    if (result != null)
                    {
                        _logger.LogTrace("Invoking result: {type}", result.GetType().FullName);
                        await result.ExecuteAsync(context);
                    }

                    return;
                }
            }
            catch (Exception ex)
            {
                await events.RaiseAsync(new UnhandledExceptionEvent(ex));

                _logger.LogCritical(ex, "Unhandled exception: {exception}", ex.Message);
                throw;
            }

            await _next(context);
        }
Exemple #7
0
        public async Task Invoke(HttpContext context, IEndpointRouter router /*, IUserSession session, IEventService events*/)
        {
            try {
                IEndpointHandler endpoint = router.Find(context);
                if (endpoint != null)
                {
                    _logger.LogInformation("Invoking IdentityServer endpoint: {endpointType} for {url}", endpoint.GetType().FullName, context.Request.Path.ToString());

                    IEndpointResult result = await endpoint.ProcessAsync(context);

                    if (result != null)
                    {
                        _logger.LogTrace("Invoking result: {type}", result.GetType().FullName);
                        await result.ExecuteAsync(context);
                    }

                    return;
                }
            } catch (Exception ex) {
                //await events.RaiseAsync(new UnhandledExceptionEvent(ex));
                _logger.LogCritical(ex, "Unhandled exception: {exception}", ex.Message);
                throw;
            }

            await _next(context);
        }
Exemple #8
0
        public async Task InvokeAsync()
        {
            ActionResult result   = null;
            bool         executed = false;

            try
            {
                _logger.LogInformation("C# HTTP trigger Authority function processed a request.");
                var context         = _httpContextAccessor.HttpContext;
                var endpointHandler = _endpointRouter.Find(_httpContextAccessor.HttpContext);
                if (endpointHandler != null)
                {
                    _logger.LogInformation("Invoking IdentityServer endpoint: {endpointType} for {url}",
                                           endpointHandler.GetType().FullName, context.Request.Path.ToString());

                    _logger.LogInformation(
                        $"Found IdentityServer endpoint {_httpContextAccessor.HttpContext.Request.Path}.");
                    var endpointResult = await endpointHandler.ProcessAsync(_httpContextAccessor.HttpContext);

                    var endpointResult2 = endpointResult as IEndpointResult2;

                    if (endpointResult2 != null)
                    {
                        /*
                         * More effective if we get the object value directly from the endpointResult.
                         * Need to ask IdentityServer to expose that inner value.
                         * I did it temporarily by modifying the IdentityServer4 code by added the IEndpointResult2
                         * interface to the endpoint results.
                         */
                        _logger.LogTrace("Invoking result: {type}", endpointResult2.GetType().FullName);
                        await endpointResult2.ExecuteAsync(_httpContextAccessor.HttpResponseMessage);

                        executed = true;
                    }

                    /*
                     *  var json = "";
                     *
                     *  await endpointResult.ExecuteAsync(_httpContextAccessor.HttpContext);
                     *
                     *  _httpContextAccessor.HttpContext.Response.Body.Seek(0, SeekOrigin.Begin);
                     *  StreamReader rdr = new StreamReader(_httpContextAccessor.HttpContext.Response.Body, Encoding.UTF8);
                     *  json = rdr.ReadToEnd();
                     */
                }
            }
            catch (Exception ex)
            {
                await _events.RaiseAsync(new UnhandledExceptionEvent(ex));

                _logger.LogCritical(ex, "Unhandled exception: {exception}", ex.Message);
                throw;
            }

            if (!executed)
            {
                _httpContextAccessor.HttpResponseMessage.StatusCode = HttpStatusCode.NotFound;
            }
        }
        public async Task Invoke(HttpContext context, IEndpointRouter router, IdentityServerContext idSvrContext)
        {
            var endpoint = router.Find(context);
            if (endpoint != null)
            {
                var result = await endpoint.ProcessAsync(idSvrContext);

                if (result != null)
                {
                    await result.ExecuteAsync(idSvrContext);
                }

                return;
            }

            await _next(context);
        }
        public async Task Invoke(HttpContext context, IEndpointRouter router, IdentityServerContext idSvrContext)
        {
            var endpoint = router.Find(context);

            if (endpoint != null)
            {
                var result = await endpoint.ProcessAsync(idSvrContext);

                if (result != null)
                {
                    await result.ExecuteAsync(idSvrContext);
                }

                return;
            }

            await _next(context);
        }
        public async Task Invoke(
            HttpContext context,
            IEndpointRouter router)
        {
            try
            {
                var endpoint = router.Find(context);
                if (endpoint != null)
                {
                    _logger.LogInformation("Invoking IdentityServer endpoint: {endpointType} for {url}", endpoint.GetType().FullName, context.Request.Path.ToString());

                    var result = await endpoint.ProcessAsync(context);

                    if (result != null)
                    {
                        _logger.LogTrace("Invoking result: {type}", result.GetType().FullName);
                        var state = new ResponseState
                        {
                            EndpointResult = result,
                            HttpContext    = context,
                            CookieItems    = new Dictionary <string, string>()
                        };

                        state.CookieItems["herb"] = "stahl";
                        context.Response.OnStarting(OnStartingCallBack, state);
                    }

                    return;
                }
            }
            catch (Exception ex)
            {
                _logger.LogCritical(ex, "Unhandled exception: {exception}", ex.Message);
                throw;
            }

            await _next(context);
        }
Exemple #12
0
    /// <summary>
    /// Invokes the middleware.
    /// </summary>
    /// <param name="context">The context.</param>
    /// <param name="router">The router.</param>
    /// <param name="userSession">The user session.</param>
    /// <param name="events">The event service.</param>
    /// <param name="issuerNameService">The issuer name service</param>
    /// <param name="sessionCoordinationService"></param>
    /// <returns></returns>
    public async Task Invoke(
        HttpContext context,
        IEndpointRouter router,
        IUserSession userSession,
        IEventService events,
        IIssuerNameService issuerNameService,
        ISessionCoordinationService sessionCoordinationService)
    {
        // this will check the authentication session and from it emit the check session
        // cookie needed from JS-based signout clients.
        await userSession.EnsureSessionIdCookieAsync();

        context.Response.OnStarting(async() =>
        {
            if (context.GetSignOutCalled())
            {
                _logger.LogDebug("SignOutCalled set; processing post-signout session cleanup.");

                // this clears our session id cookie so JS clients can detect the user has signed out
                await userSession.RemoveSessionIdCookieAsync();

                var user = await userSession.GetUserAsync();
                if (user != null)
                {
                    var session = new UserSession
                    {
                        SubjectId   = user.GetSubjectId(),
                        SessionId   = await userSession.GetSessionIdAsync(),
                        DisplayName = user.GetDisplayName(),
                        ClientIds   = (await userSession.GetClientListAsync()).ToList(),
                        Issuer      = await issuerNameService.GetCurrentAsync()
                    };
                    await sessionCoordinationService.ProcessLogoutAsync(session);
                }
            }
        });

        try
        {
            var endpoint = router.Find(context);
            if (endpoint != null)
            {
                var endpointType = endpoint.GetType().FullName;

                using var activity = Tracing.BasicActivitySource.StartActivity("IdentityServerProtocolRequest");
                activity?.SetTag(Tracing.Properties.EndpointType, endpointType);

                LicenseValidator.ValidateIssuer(await issuerNameService.GetCurrentAsync());

                _logger.LogInformation("Invoking IdentityServer endpoint: {endpointType} for {url}", endpointType, context.Request.Path.ToString());

                var result = await endpoint.ProcessAsync(context);

                if (result != null)
                {
                    _logger.LogTrace("Invoking result: {type}", result.GetType().FullName);
                    await result.ExecuteAsync(context);
                }

                return;
            }
        }
        catch (Exception ex)
        {
            await events.RaiseAsync(new UnhandledExceptionEvent(ex));

            _logger.LogCritical(ex, "Unhandled exception: {exception}", ex.Message);
            throw;
        }

        await _next(context);
    }