注册IAuthenticationManager用简单的喷油器 [英] Register IAuthenticationManager with Simple Injector

查看:211
本文介绍了注册IAuthenticationManager用简单的喷油器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有单纯性喷油器哪里我都感动了所有我注册的,以OWIN管道的配置设置。

I am having a configuration setup for Simple Injector where I have moved all of my registrations to OWIN pipeline.

现在的问题是,我有一个控制器的AccountController 这实际上需要参数

Now the problem is I have a controller AccountController which actually takes parameters as

public AccountController(
    AngularAppUserManager userManager, 
    AngularAppSignInManager signinManager, 
    IAuthenticationManager authenticationManager)
{
    this._userManager = userManager;
    this._signInManager = signinManager;
    this._authenticationManager = authenticationManager;
}

现在我Owin管道的配置看起来像这样

Now my Owin Pipeline configurations looks something like this

public void Configure(IAppBuilder app)
{
    _container = new Container();
    ConfigureOwinSecurity(app);
    ConfigureWebApi(app);
    ConfigureSimpleinjector(_container);

    app.Use(async (context, next) =>
    {
        _container.Register<IOwinContext>(() => context);
        await next();
    });

    _container.Register<IAuthenticationManager>(
        () => _container.GetInstance<IOwinContext>().Authentication);

    _container.Register<SignInManager<Users, Guid>, AngularAppSignInManager>();
}

private static void ConfigureOwinSecurity(IAppBuilder app)
{
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        CookieName = "AppNgCookie",
        //LoginPath = new PathString("/Account/Login")
    });
}

private static void ConfigureWebApi(IAppBuilder app)
{
    HttpConfiguration config = new HttpConfiguration();
    WebApiConfig.Register(config);
    app.UseWebApi(config);
}

private static void ConfigureSimpleinjector(Container container)
{
    SimpleInjectorInitializer.Initialize(container);
}

和简单的喷油器初始化程序看起来像这样

And Simple Injector Initializer looks something like this

private static void InitializeContainer(Container container)
{
    container.Register<DbContext, AngularAppContext>();

    container.Register<IUserStore<Users, Guid>, AngularAppUserStore>();
    container.Register<IRoleStore<Roles, Guid>, AngularAppRoleStore>();

    container.Register<UserManager<Users, Guid>, AngularAppUserManager>();
    container.Register<RoleManager<Roles, Guid>, AngularAppRoleManager>();
    //container.RegisterPerWebRequest<SignInManager<Users, Guid>, AngularAppSignInManager>();

    container.Register<IdentityFactoryOptions<AngularAppUserManager>, IdentityFactoryOptions<AngularAppUserManager>>();
    //container.Register<IAuthenticationManager>(() => HttpContext.Current.GetOwinContext().Authentication);

    //container.Register<SignInManager<Users, Guid>, AngularAppSignInManager>();
    // For instance:
    // container.Register<IUserRepository, SqlUserRepository>();
}

现在的问题是,该控制器无法注册 IAuthenticationManager 。我试图用

Now the problem is The controller is not able to register IAuthenticationManager. I tried using

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

但是,这令我异常为:

But that Leaves me with Exception as:

System.InvalidOperationException:没有owin.Environment项目被发现
  在上下文

System.InvalidOperationException: No owin.Environment item was found in the context.

在此行

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

我也试过,而不是使用 HttpContext.Current.GetOwinContext()的。验证上面的中提到的配置公共无效配置(应用程序)方法使用注册 app.use 。再后来通过容器解决它,以获得 IAuthenticationManager 。但每一个可能性都留下了我的失败。

I also tried instead of using HttpContext.Current.GetOwinContext().Authentication with the configuration mentioned above in public void Configure(app) method to register using app.use. And then later resolve it via container to get the IAuthenticationManager. But every possibilities have left me failed.

我缺少的是在这里吗?为什么 HttpContext.Current.GetOwinContext()。Authentcation 未能从OwinContext解决authentcation?

What am I missing here? Why HttpContext.Current.GetOwinContext().Authentcation is failing to resolve authentcation from OwinContext?

如果那不是,为什么通过 app.Use 同样的配置也不能正常工作?

And if thats not, why is the same configuration via app.Use also not working?

推荐答案

在与 IAuthenticationManager 干什么注册<一个href=\"https://github.com/trailmax/ClaimsAuthorisation/blob/master/ClaimsAuth/App_Start/SimpleInjector.cs\">worked我的,没有问题。在某些时候,我得到相同的异常,你都拿到,但由符合

What you are doing with IAuthenticationManager registration worked for me with no issues. At some point I was getting the same exception as you were getting, but that was caused by line with

container.Verify();

只是容器配置后。它试图创建注册的对象的所有实例,但没有 HttpContext.Current present,因此例外。

just after the container configuration. It was trying to create all instances of registered objects, but there was no HttpContext.Current present, hence the exception.

您没有任何HTTP请求之前走出容器的任何实例可用?如果你真的需要它们,然后上班的唯一途径解决办法是利用厂,由NightOwl888的建议。如果你没有在HTTP请求之前需要容器,然后重构,所以它不是outwith HTTP请求使用。

Are you not getting any instances out of container before any HTTP request is available? If you really need them, then the only way to work around this is use Factory, as suggested by NightOwl888. If you don't need container before the HTTP request, then refactor, so it is not use outwith HTTP request.

这篇关于注册IAuthenticationManager用简单的喷油器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆