简体   繁体   English

如何在ASP.NET 5中使用基于IAppBuilder的Owin中间件

[英]How to use IAppBuilder-based Owin Middleware in ASP.NET 5

ASP.NET 5 (aspnet vnext) is OWIN based like Katana was, but has different abstractions. ASP.NET 5(aspnet vnext)是基于OWIN的,像Katana一样,但有不同的抽象。 Notably IAppBuilder has been replaced by IApplicationBuilder . 值得注意的是, IAppBuilder已被IApplicationBuilder取代。 Many middleware libraries depend on IAppBuilder and have not been updated to support ASP.NET 5 许多中间件库依赖于IAppBuilder并且尚未更新以支持ASP.NET 5

How can I use this OWIN middleware in APS.NET 5 middleware. 如何在APS.NET 5中间件中使用此OWIN中间件。 Both are OWIN based so it should be possible. 两者都是基于OWIN的,所以它应该是可能的。

Microsoft.AspNet.Builder.OwinExtensions does provide a UseOwin method, but it is based on the low-level OWIN signatures so cannot be used with methods expecting IAppBuilder . Microsoft.AspNet.Builder.OwinExtensions确实提供了UseOwin方法,但它基于低级别的OWIN签名,因此不能与期望IAppBuilder方法一起使用。

Edit: you can now use the AspNet.Hosting.Katana.Extensions package for that. 编辑:您现在可以使用AspNet.Hosting.Katana.Extensions


Here's a slightly different version, that uses AppBuilder.DefaultApp : 这是一个略有不同的版本,使用AppBuilder.DefaultApp

public static IApplicationBuilder UseOwinAppBuilder(this IApplicationBuilder app, Action<IAppBuilder> configuration)
{
    if (app == null)
    {
        throw new ArgumentNullException(nameof(app));
    }

    if (configuration == null)
    {
        throw new ArgumentNullException(nameof(configuration));
    }

    return app.UseOwin(setup => setup(next =>
    {
        var builder = new AppBuilder();
        var lifetime = (IApplicationLifetime) app.ApplicationServices.GetService(typeof(IApplicationLifetime));

        var properties = new AppProperties(builder.Properties);
        properties.AppName = app.ApplicationServices.GetApplicationUniqueIdentifier();
        properties.OnAppDisposing = lifetime.ApplicationStopping;
        properties.DefaultApp = next;

        configuration(builder);

        return builder.Build<Func<IDictionary<string, object>, Task>>();
    }));
}

Note that referencing Microsoft.Owin makes your app incompatible with dnxcore50 (Core CLR). 请注意,引用Microsoft.Owin会使您的应用与dnxcore50 (Core CLR)不兼容。

The oft cited reference that the frameworks are compatible is an extension method build by Thinktecture for supporting their IdentityServer3 on ASP.NET 5 . 经常提到框架兼容的参考是Thinktecture为在ASP.NET 5上支持IdentityServer3而构建的扩展方法

That method is specific to IdentityServer and does not support the request being processed by any middleware registered later in the AspNet pipeline (it does not call the next component). 该方法特定于IdentityServer,并且不支持稍后在AspNet管道中注册的任何中间件处理的请求(它不调用下一个组件)。

This adapts the method to address those shortcomings: 这使得该方法能够解决这些缺点:

internal static class IApplicationBuilderExtensions
{
  public static void UseOwin(
    this IApplicationBuilder app,
    Action<IAppBuilder> owinConfiguration )
  {
    app.UseOwin(
      addToPipeline =>
        {
          addToPipeline(
            next =>
              {
                var builder = new AppBuilder();

                owinConfiguration( builder );

                builder.Run( ctx => next( ctx.Environment ) );

                Func<IDictionary<string, object>, Task> appFunc =
                  (Func<IDictionary<string, object>, Task>)
                  builder.Build( typeof( Func<IDictionary<string, object>, Task> ) );

                return appFunc;
              } );
        } );
  }
}

It can be used as follows: 它可以使用如下:

app.UseOwin(
    owin =>
        {
            // Arbitrary IAppBuilder registrations can be placed in this block

            // For example, this extension can be provided by
            // NWebsec.Owin or Thinktecture.IdentityServer3
            owin.UseHsts();
        } );

// ASP.NET 5 components, like MVC 6, will still process the request
// (assuming the request was not handled by earlier middleware)
app.UseMvc();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM