简体   繁体   English

简单的喷射器注册ASP.NET身份和Web API

[英]Simple Injector Registration ASP.NET identity & Web API

There is some amount of confusion regarding usage of Asp .net and Identity when put together with Simple Injector. 与Simple Injector一起使用时,关于Asp .net和Identity的用法存在一些困惑。 There are some articles about how OWIN is anti-pattern and how bad the asp .net basic template is!! 有一些关于OWIN如何反模式以及asp .net基本模板有多糟糕的文章!

Regardless, the final issue as I understand is to make sure we inject the right dependencies while adhering to the OWIN request pipeline when a user identity token is being generated. 无论如何,据我了解,最后一个问题是确保在生成用户身份令牌时,在遵守OWIN请求管道的同时注入正确的依赖项。

I am possibly not yet well versed with Simple Injector or OWIN, but I tried manually registering AccountController by overriding the simple injector registration. 我可能还不熟悉Simple Injector或OWIN,但是我尝试通过覆盖简单的喷射器注册来手动注册AccountController。

Can some one confirm if the following code is correct? 有人可以确认以下代码是否正确吗? I can see that it works in Visual studio with Register and Login. 我可以看到它在Visual Studio中具有“注册”和“登录”功能。 I can get tokens and authenticate myself for different logins with the same process. 我可以使用相同的过程获取令牌并为不同的登录进行身份验证。 But want to be sure if this is correct before using in a project. 但是要在项目中使用之前确定这是否正确。

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var container = new Container();
        container.Options.DefaultScopedLifestyle = new WebApiRequestLifestyle();
        container.RegisterWebApiControllers(GlobalConfiguration.Configuration);
        container.Options.AllowOverridingRegistrations = true;
        ApplicationUserManager um = new ApplicationUserManager(
            new UserStore<ApplicationUser>(new ApplicationDbContext()));
        SecureDataFormat<AuthenticationTicket> sdt = 
            new SecureDataFormat<AuthenticationTicket>(
                new TicketSerializer(), 
                new DpapiDataProtectionProvider().Create("ASP.NET Identity"), 
                TextEncodings.Base64);
        container.Register<AccountController>(
            () => new AccountController(um, sdt), Lifestyle.Singleton);
        container.Options.AllowOverridingRegistrations = false;

        container.Verify();
        app.Use(async (context, next) =>
        {
            using (container.BeginExecutionContextScope())
            {
                await next();
            }
        });
        GlobalConfiguration.Configuration.DependencyResolver =
            new SimpleInjectorWebApiDependencyResolver(container);
        ConfigureAuth(app);
    }

I see one error and that is that both your ApplicationDbContext and AccountController are becoming singletons. 我看到一个错误,那就是您的ApplicationDbContextAccountController都变得单例。 This might be something you wont notice during development, but this will cause problems in production. 在开发过程中您可能不会注意到这一点,但这会在生产中引起问题。 MVC will throw an exception when you reuse the same controller instance on a new request and the data in DbContext will become stale very quickly and a DbContext is not thread-safe. 当您在新请求上重用同一控制器实例时,MVC会引发异常,并且DbContext的数据将很快变得陈旧,并且DbContext不是线程安全的。 Instead, you can do the following: 相反,您可以执行以下操作:

var sdt = new SecureDataFormat<AuthenticationTicket>(
    new TicketSerializer(), 
    new DpapiDataProtectionProvider().Create("ASP.NET Identity"), 
    TextEncodings.Base64);

container.Options.AllowOverridingRegistrations = true;

container.Register<AccountController>(() => 
    new AccountController(
        new ApplicationUserManager(
            new UserStore<ApplicationUser>(new ApplicationDbContext())), 
        sdt),
    Lifestyle.Scoped);

container.Options.AllowOverridingRegistrations = false;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM