首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在ASP.NET 5中使用基于IAppBuilder的Owin中间件

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

Stack Overflow用户
提问于 2015-06-10 04:18:10
回答 2查看 24.6K关注 0票数 20

ASP.NET 5 (aspnet vnext)像Katana一样基于OWIN,但具有不同的抽象。值得注意的是,IApplicationBuilder已经取代了IAppBuilder。许多中间件库依赖于IAppBuilder,尚未更新为支持ASP.NET 5

如何在APS.NET 5中间件中使用该中间件。两者都是基于OWIN的,所以这应该是可能的。

Microsoft.AspNet.Builder.OwinExtensions确实提供了UseOwin方法,但它基于低级OWIN签名,因此不能与需要IAppBuilder的方法一起使用。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-12 04:47:00

编辑:您现在可以使用AspNet.Hosting.Katana.Extensions package进行编辑。

这是一个稍微不同的版本,它使用了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>>();
    }));
}

请注意,引用Microsoft.Owin会使您的应用程序与dnxcore50 (Core CLR)不兼容。

票数 12
EN

Stack Overflow用户

发布于 2015-06-10 04:18:10

框架兼容的最常被引用的参考文献是an extension method build by Thinktecture for supporting their IdentityServer3 on ASP.NET 5

该方法特定于IdentityServer,并且不支持由稍后在AspNet管道中注册的任何中间件处理的请求(它不调用下一个组件)。

这使该方法适合于解决这些缺点:

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;
              } );
        } );
  }
}

它的使用方法如下:

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();
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30742028

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档