/// <summary>
        /// Resolves the <see cref="IServiceProvider"/> instance from the OWIN context
        /// and creates a new instance of the <see cref="OpenIddictServerOwinMiddleware"/> class,
        /// which is used to register <see cref="OpenIddictServerOwinHandler"/> in the pipeline.
        /// </summary>
        /// <param name="context">The <see cref="IOwinContext"/>.</param>
        /// <returns>
        /// A <see cref="ValueTask"/> that can be used to monitor the asynchronous operation.
        /// </returns>
        public override Task Invoke([NotNull] IOwinContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var provider = context.Get <IServiceProvider>(typeof(IServiceProvider).FullName);

            if (provider == null)
            {
                throw new InvalidOperationException(new StringBuilder()
                                                    .Append("No service provider was found in the OWIN context. For the OpenIddict server ")
                                                    .Append("services to work correctly, a per-request 'IServiceProvider' must be attached ")
                                                    .AppendLine("to the OWIN environment with the dictionary key 'System.IServiceProvider'.")
                                                    .Append("Note: when using a dependency injection container supporting middleware resolution ")
                                                    .Append("(like Autofac), the 'app.UseOpenIddictServer()' extension MUST NOT be called.")
                                                    .ToString());
            }

            // Note: the Microsoft.Extensions.DependencyInjection container doesn't support resolving services
            // with arbitrary parameters, which prevents the server OWIN middleware from being resolved directly
            // from the DI container, as the next middleware in the pipeline cannot be specified as a parameter.
            // To work around this limitation, the server OWIN middleware is manually instantiated and invoked.
            var middleware = new OpenIddictServerOwinMiddleware(
                next: Next,
                options: GetRequiredService <IOptionsMonitor <OpenIddictServerOwinOptions> >(provider),
                dispatcher: GetRequiredService <IOpenIddictServerDispatcher>(provider),
                factory: GetRequiredService <IOpenIddictServerFactory>(provider));

            return(middleware.Invoke(context));
예제 #2
0
        /// <summary>
        /// Resolves the <see cref="IServiceProvider"/> instance from the OWIN context
        /// and creates a new instance of the <see cref="OpenIddictServerOwinMiddleware"/> class,
        /// which is used to register <see cref="OpenIddictServerOwinHandler"/> in the pipeline.
        /// </summary>
        /// <param name="context">The <see cref="IOwinContext"/>.</param>
        /// <returns>
        /// A <see cref="ValueTask"/> that can be used to monitor the asynchronous operation.
        /// </returns>
        public override Task Invoke(IOwinContext context)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var provider = context.Get <IServiceProvider>(typeof(IServiceProvider).FullName);

            if (provider is null)
            {
                throw new InvalidOperationException(SR.GetResourceString(SR.ID0121));
            }

            // Note: the Microsoft.Extensions.DependencyInjection container doesn't support resolving services
            // with arbitrary parameters, which prevents the server OWIN middleware from being resolved directly
            // from the DI container, as the next middleware in the pipeline cannot be specified as a parameter.
            // To work around this limitation, the server OWIN middleware is manually instantiated and invoked.
            var middleware = new OpenIddictServerOwinMiddleware(
                next: Next,
                options: GetRequiredService <IOptionsMonitor <OpenIddictServerOwinOptions> >(provider),
                dispatcher: GetRequiredService <IOpenIddictServerDispatcher>(provider),
                factory: GetRequiredService <IOpenIddictServerFactory>(provider));

            return(middleware.Invoke(context));