使用带有IdentityFactory中间件的IOC容器
我试图理解UserManagerFactory中间件,这里解释了usermanager的每个请求生命周期管理 。
我创建了这个我从Startup Configuration方法调用的类
public class CustomUserManagerProvider { public static CustomUserStore CreateCustomUserStore() { return new CustomUserStore(/*Need to inject dependencies here*/); } public static CustomUserManager CreateCustomUserManager(IdentityFactoryOptions options, IOwinContext context) { return new CustomUserManager(context.Get<CustomUserStore>()); } }
和启动配置
public void Configuration(IAppBuilder app) { app.CreatePerOwinContext(CustomUserManagerProvider.CreateCustomUserStore); app.CreatePerOwinContext(CustomUserManagerProvider.CreateCustomUserManager); ////....Other things }
现在,我的CustomUserStore有一些依赖项,我想在构造函数中注入。
IOC容器的组合根知道如何解决这些依赖关系。
如何CustomUserManagerProvider
DI容器(如果有意义的话)……
虽然这很有效
public static CustomUserStore CreateCustomUserStore() { var dependency = DependencyResolver.Current.GetService(); return new CustomUserStore(dependency); }
但是,我试图避免服务定位器(反)模式。 这是我唯一的选择吗,这是对的吗?
我正在使用Ninject。
我只是在组合根中的requestScope中创建一个UserManager并注入到控制器中,这不是一回事吗?
在Web应用程序中,CreatePerOwinContext与创建InRequestScope相同吗?
是的,如果您愿意,可以以不同的方式将UserManager注入控制器,没有什么需要使用CreatePerOwinContext / IdentityFactoryMiddleware。
- 需要将Asp.Net Identity(RTM)用户ID从nvarchar(128)更改为uniqueidentifier
- 我如何创建UserManager的实例
- 使用Azure AD时,将用户重定向到自定义登录页面
- 具有特殊字符的ASP.NET MVC标识电子邮件/用户名
- Asp.Net Identity 2.0 – 如何使用SmtpClient实现IIdentityMessageService来执行异步SMTP?
- Owin Authentication.SignIn不工作
- UserManager错误 – 在上一个异步操作完成之前,在此上下文中启动了第二个操作
- 错误:“Id字段是必需的。”在使用AspNet.Identity 2.0在数据库中注册新用户时
- 在ASP.NET中的IdentityUserRole 2.0中获取角色名称