12

I need to build an Owin middle-ware object but not from within the Startup class. I need to build it from within anywhere else in my code, so I need a reference to the AppBuilder instance of the application. Is there a way to get that from anywhere else?

Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
  • how did you manage to persist what you build in subsequent requests? I created User Manager per OWIN context once the user logs in, but then it's empty when navigating to some other MVC controller. – Aseem Chiplonkar Mar 07 '16 at 21:35

1 Answers1

17

You could simply inject AppBuilder itself to OwinContext. But since Owin context only supports IDisposable object, wrap it in IDisposable object and register it.

public class AppBuilderProvider : IDisposable
{
    private IAppBuilder _app;
    public AppBuilderProvider(IAppBuilder app)
    {
        _app = app;
    }
    public IAppBuilder Get() { return _app; }
    public void Dispose(){}
}

public class Startup
{
    // the startup method
    public void Configure(IAppBuilder app)
    {
        app.CreatePerOwinContext(() => new AppBuilderProvider(app));
        // another context registrations
    }
}

So in everywhere of your code you have access IAppBuilder object.

public class FooController : Controller
{
    public ActionResult BarAction()
    {
        var app = HttpContext.Current.GetOwinContext().Get<AppBuilderProvider>().Get();
        // rest of your code.        
    }
}
Obi
  • 3,091
  • 4
  • 34
  • 56
Sam FarajpourGhamari
  • 14,601
  • 4
  • 52
  • 56
  • Brilliant! Why didn't I think of that? :-) – Water Cooler v2 Aug 10 '15 at 11:28
  • 1
    The code within FooController doesn't work. ```IOwinContext.Get``` doesnt have a method that has no parameters, you must supply a key. And that key is some bizarre key created internally by ```CreatePerOwinContext()``` that starts with ```"AspNet.Identity.Own:"``` and the Assembly qualified type name, ie ```typeof(yourtype).AssemblyQualifiedName```. See my answer over here at: http://stackoverflow.com/a/36044219/323456 – enorl76 Mar 16 '16 at 18:41
  • 1
    @enorl76 The `Get` method I have used in this example is an extension method which is implemented in `Microsoft.AspNet.Identity.Owin` assembly. So if you added relevant package to your project and add `Microsoft.AspNet.Identity.Owin` namespace in your file you could use this method as well. – Sam FarajpourGhamari Mar 16 '16 at 19:14
  • 1
    @SamFarajpourGhamari this is why i really don't like extension methods. thanks for the tip. – enorl76 Mar 21 '16 at 19:19
  • Bringing this old thread back to life. I followed the steps from the answer. So while everything goes well in terms of adding the middleware but the next request does seem to be finding the middleware in the pipeline. just to give a context, in my case it's a bearer token auth middleware. So when i see a 401, i add the middleware (not sure if that's OK to do in the first place)n but my next request still gets 401. The tokens are ok, so it is the middleware that is still missing. – Dibzmania Jun 16 '20 at 07:33