Как получить OwinContext из Global.asax?

Я пытаюсь настроить Injection Dependency, и мне нужно ввести IAuthenticationManager из идентификатора ASP.NET в OwinContext.

Для этого я из моего Global.asax -> ServiceConfig.Configure() работает:

 container.Register(() => HttpContext.Current.GetOwinContext().Authentication);

Но когда я запускаю свое приложение, я получаю это сообщение:

Нет объекта owin.Environment был найден в контексте

Почему этот HttpContext.Current.GetOwinContext() недоступен из Global.asax?

Startup.cs

[assembly: OwinStartupAttribute(typeof(MyApp.Web.Startup))]
namespace Speedop.Web
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
    }
}

Startup.Auth.cs

public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<UserManager<User, int>, User, int>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentityCallback: (manager, user) => manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie),
                    getUserIdCallback: (id) => (Int32.Parse(id.GetUserId()))
                    )
            }
        });

        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
    }
}

Ответ 1

Я исправил это следующим образом:

container.RegisterPerWebRequest(() =>
{
    if (HttpContext.Current != null && HttpContext.Current.Items["owin.Environment"] == null && container.IsVerifying())
    {
        return new OwinContext().Authentication;
    }
    return HttpContext.Current.GetOwinContext().Authentication;

});

Похоже, что OwnContext отсутствует при запуске, поэтому я буду ждать его и вводить его после его присутствия. Обратите внимание, что container.IsVerifying() присутствует в SimpleInjector.Advanced