C# 简单的注入器注册ASP.NET标识&;Web API
当与SimpleInjector结合使用时,在Asp.net和Identity的使用方面存在一些混乱。有一些文章介绍了OWIN是如何反模式的,以及asp.net基本模板是如何糟糕 无论如何,据我所知,最后一个问题是确保在生成用户标识令牌时,在遵守OWIN请求管道的同时注入正确的依赖项 我可能还不太熟悉Simple Injector或OWIN,但我尝试通过覆盖Simple Injector注册来手动注册AccountController 有人能确认以下代码是否正确吗?我可以看到它在visualstudio中使用Register和Login。我可以使用相同的过程获取令牌并对不同的登录进行身份验证。但在项目中使用之前,您需要确定这是否正确C# 简单的注入器注册ASP.NET标识&;Web API,c#,asp.net-web-api,asp.net-identity,owin,simple-injector,C#,Asp.net Web Api,Asp.net Identity,Owin,Simple Injector,当与SimpleInjector结合使用时,在Asp.net和Identity的使用方面存在一些混乱。有一些文章介绍了OWIN是如何反模式的,以及asp.net基本模板是如何糟糕 无论如何,据我所知,最后一个问题是确保在生成用户标识令牌时,在遵守OWIN请求管道的同时注入正确的依赖项 我可能还不太熟悉Simple Injector或OWIN,但我尝试通过覆盖Simple Injector注册来手动注册AccountController 有人能确认以下代码是否正确吗?我可以看到它在visualst
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);
}
公共部分类启动
{
公共无效配置(IAppBuilder应用程序)
{
var container=新容器();
container.Options.DefaultScopedLifestyle=新的WebApiRequestLifestyle();
容器.RegisterWebApp控制器(全局配置.Configuration);
container.Options.AllowOverridingRegistrations=true;
ApplicationUserManager um=新的ApplicationUserManager(
新的用户存储(新的ApplicationDbContext());
SecureDataFormat sdt=
新的SecureDataFormat(
新的票证序列化程序(),
新建DpapiDataProtectionProvider().Create(“ASP.NET标识”),
文本编码(Base64);
集装箱。登记(
()=>新的AccountController(嗯,sdt),生活方式。单身人士);
container.Options.AllowOverridingRegistrations=false;
container.Verify();
应用程序使用(异步(上下文,下一步)=>
{
使用(container.BeginExecutionContextScope())
{
等待下一个();
}
});
GlobalConfiguration.Configuration.DependencyResolver=
新的SimpleInjectorWebApidencyResolver(容器);
ConfigureAuth(app);
}
我看到一个错误,那就是您的ApplicationDbContext
和AccountController
都变成了单例。这可能是您在开发过程中不会注意到的,但这会在生产中造成问题。当您在新请求上重用同一控制器实例时,MVC将引发异常,并且DbContext
中的ata将很快过时,并且DbContext
不是线程安全的。您可以执行以下操作:
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;
var sdt=新的SecureDataFormat(
新的票证序列化程序(),
新建DpapiDataProtectionProvider().Create(“ASP.NET标识”),
文本编码(Base64);
container.Options.AllowOverridingRegistrations=true;
容器。寄存器(()=>
新会计控制员(
新应用程序服务器管理器(
新的用户存储(新的ApplicationDbContext()),
sdt),
生活方式(范围);
container.Options.AllowOverridingRegistrations=false;
是的,正确。谢谢。虽然我必须做一个小小的更改。默认的生活方式被设置为瞬态,这导致了一个配置错误,瞬态对象无法实现IDisposable。因此,在我更改为生活方式后。在容器中确定范围。注册,看起来很好。但是感谢您发现请求问题,这将已经造成了严重的破坏。