Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

ASP.NET 5 (ASP.NET vNext) is OWIN based like Katana was, but has different abstractions. Notably IAppBuilder has been replaced by IApplicationBuilder. Many middleware libraries depend on IAppBuilder and have not been updated to support ASP.NET 5

How can I use this OWIN middleware in ASP.NET 5 middleware. Both are OWIN based so it should be possible.

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.

like image 679
vossad01 Avatar asked Jun 09 '15 20:06

vossad01


People also ask

What is an OWIN middleware component?

OWIN allows web apps to be decoupled from web servers. It defines a standard way for middleware to be used in a pipeline to handle requests and associated responses. ASP.NET Core applications and middleware can interoperate with OWIN-based applications, servers, and middleware.

How do I set up OWIN?

Create an ASP.NET Web App using OWIN Startup Add an OWIN startup class. In Visual Studio 2017 right-click the project and select Add Class. - In the Add New Item dialog box, enter OWIN in the search field, and change the name to Startup. cs, and then select Add.

Does OWIN use IIS?

OWIN is an abstraction between the web application and the hosting platform. If you write your web application using OWIN you are not tied to IIS, you can use another host if you like. You asked why use OWIN rather than IIS, but these are not alternatives to each other.


2 Answers

Edit: you can now use the AspNet.Hosting.Katana.Extensions package for that.


Here's a slightly different version, that uses 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).

like image 111
Kévin Chalet Avatar answered Oct 02 '22 03:10

Kévin Chalet


The oft cited reference that the frameworks are compatible is an extension method build by Thinktecture for supporting their IdentityServer3 on ASP.NET 5.

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).

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();
like image 34
vossad01 Avatar answered Oct 02 '22 05:10

vossad01