使用 Simple Injector 注册 IAuthenticationManager [英] Register IAuthenticationManager with Simple Injector

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

问题描述

我正在为 Simple Injector 进行配置设置,我已将所有注册移至 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 Pipeline 配置看起来像这样

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);
}

Simple Injector Initializer 看起来像这样

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

但这让我有例外:

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().Authentication 和上面提到的配置在 public void Configure(app) 方法中使用 注册>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 解析身份验证?

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 你正在做什么注册 为我工作没有任何问题.在某些时候,我遇到了与您相同的异常,但这是由

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 存在,因此出现异常.

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 请求可用之前,您是否没有从容器中取出任何实例?如果你真的需要它们,那么解决这个问题的唯一方法就是使用 Factory,正如 NightOwl888 所建议的那样.如果在 HTTP 请求之前不需要容器,则进行重构,因此它不会与 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.

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

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