/// <summary> /// Resolves the <see cref="IServiceProvider"/> instance from the OWIN context /// and creates a new instance of the <see cref="OpenIddictValidationOwinMiddleware"/> class, /// which is used to register <see cref="OpenIddictValidationOwinHandler"/> 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 validation ") .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.UseOpenIddictValidation()' extension MUST NOT be called.") .ToString()); } // Note: the Microsoft.Extensions.DependencyInjection container doesn't support resolving services // with arbitrary parameters, which prevents the validation 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 validation OWIN middleware is manually instantiated and invoked. var middleware = new OpenIddictValidationOwinMiddleware( next: Next, logger: GetRequiredService <ILogger <OpenIddictValidationOwinMiddleware> >(provider), options: GetRequiredService <IOptionsMonitor <OpenIddictValidationOwinOptions> >(provider), provider: GetRequiredService <IOpenIddictValidationProvider>(provider)); return(middleware.Invoke(context));
/// <summary> /// Resolves the <see cref="IServiceProvider"/> instance from the OWIN context /// and creates a new instance of the <see cref="OpenIddictValidationOwinMiddleware"/> class, /// which is used to register <see cref="OpenIddictValidationOwinHandler"/> 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.ID0168)); } // Note: the Microsoft.Extensions.DependencyInjection container doesn't support resolving services // with arbitrary parameters, which prevents the validation 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 validation OWIN middleware is manually instantiated and invoked. var middleware = new OpenIddictValidationOwinMiddleware( next: Next, options: GetRequiredService <IOptionsMonitor <OpenIddictValidationOwinOptions> >(provider), dispatcher: GetRequiredService <IOpenIddictValidationDispatcher>(provider), factory: GetRequiredService <IOpenIddictValidationFactory>(provider)); return(middleware.Invoke(context));