Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Owin running before Structure Map configuration

On an ASP.NET MVC 5 project using OWIN I have the following:

[assembly: OwinStartup(typeof(MvcProj.Site.OwinStartup), "Configure")]

namespace MvcProj.Site {

  public partial class OwinStartup {
    public void Configure(IAppBuilder application) {

      UrlHelper url = new UrlHelper(HttpContext.Current.Request.RequestContext);

      application.UseCookieAuthentication(new CookieAuthenticationOptions {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        CookieSecure = CookieSecureOption.SameAsRequest,
        LoginPath = new PathString(url.Action(MVC.User.SignIn())),
        ReturnUrlParameter = "redirect"
      });

      application.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

    } // Configure

  } // OwinStartup

}

As you can see I am defining the login path as follows:

LoginPath = new PathString(url.Action(MVC.User.SignIn())),

I get an error from StructureMap saying I do not have ITranslator defined ...

In fact it is defined but all my controllers are base on a BaseController:

public abstract class BaseController : Controller, ITranslator_ {

  public readonly ITranslator _translator;

  protected BaseController() {
    _translator = ObjectFactory.Container.GetInstance<ITranslator>();
  } // BaseController

  public String _(String value) {
    return _translator.Translate(value);
  } // _

}

So what I think it happens is that Owin runs before my IoC code in global.asax Application Start.

If I remove the code line LoginPath = new PathString(url.Action(MVC.User.SignIn())) then everything works fine.

Could someone, please, tell me how to solve this?

Thank You,

Miguel

like image 715
Miguel Moura Avatar asked Feb 20 '26 11:02

Miguel Moura


1 Answers

The Microsft.Owin.SystemWeb host uses the PreApplicationStartMethodAttribute to bootstrap itself which runs before your Application_Start method. This is why you're seeing the crash. You'll need to move your DI setup into the Startup class.

I've since switched from ASP.NET MVC to Nancy but your setup should be similar aside from the need to also setup a dependency resolver for MVC. For this you'll need to install StructureMap.MVC4 and then remove the StructuremapMvc class it adds since your setup code is now in the Startup class.

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var container = SetupStructureMap();

        // sets up the mvc dependency resolver
        DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
        GlobalConfiguration.Configuration.DependencyResolver = new StructureMapDependencyResolver(container);

        SetupAuth(app, container);
    }

    private static IContainer SetupStructureMap()
    {
        ObjectFactory.Initialize(x =>
        {
            // ...
        });

        return ObjectFactory.Container;
    }

    public static void SetupAuth(IAppBuilder app, IContainer container)
    {
        app.SetDataProtectionProvider(container.GetInstance<IDataProtectionProvider>());
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = Constants.AppAuthType,
            CookieHttpOnly = true,
            CookieName = "app.id",
            LogoutPath = new PathString("/logout"),
            Provider = container.GetInstance<ICookieAuthenticationProvider>(),
            ReturnUrlParameter = string.Empty
        });
    }
}

My Startup class is loosely based on the one from JabbR.

like image 95
Brian Surowiec Avatar answered Feb 22 '26 01:02

Brian Surowiec