dependency-injection – 如何使用unity注入ApplicationUserManager
|
我有这样定义的ApplicationUserManager: public class ApplicationUserManager : UserManager<ApplicationUser,int>
{
public ApplicationUserManager(IUserStore<ApplicationUser,int> store)
: base(store)
{
}
public override Task<IdentityResult> CreateAsync(ApplicationUser user,string password)
{
var result = base.CreateAsync(user,password);
//...
Repository.DoSomething(...); //Repository is null here
//..
}
[Dependency]
public IRepository Repository { get; set; }
}
出于某种原因我的存储库没有注入.存储库始终为空 在我的统一配置中我也有这一行 .RegisterType<ApplicationUserManager>(new HierarchicalLifetimeManager()) 如何注射? 更新N: 这是我的控制器的代码;我如何获得UserManager: public ApplicationUserManager UserManager
{
get
{
return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
public async Task<IHttpActionResult> Register(RegisterBindingModel model)
{
var user = new ApplicationUser() { UserName = model.Email,Email = model.Email };
//calling my implemintation
IdentityResult result = await UserManager.CreateAsync(...);
}
我当前的统一配置 .RegisterType<IUserStore<ApplicationUser,int>,CustomUserStore>(new HierarchicalLifetimeManager()) .RegisterType<IAuthenticationManager>(new InjectionFactory(o => HttpContext.Current.GetOwinContext().Authentication)) .RegisterType<UserManager<ApplicationUser,ApplicationUserManager>() .RegisterType<DbContext,ApplicationDbContext>(new HierarchicalLifetimeManager()) .RegisterType<AccountController>(new InjectionConstructor()) .RegisterType<ApplicationDbContext>(new HierarchicalLifetimeManager()) .RegisterType<ApplicationUserManager>() 更新N 1: 正如我发现Unity.Mvc包不是注入WebApi控制器.我用这个method来注入WebApi控制器.我试图删除Unity.Mvc包但得到了Identity抛出错误.根据我的理解,因为Unity无法实例化某些Identity类型,但是在设置Mvc容器的情况下,它们正在配置和工作. 所以我的项目中有两个容器Unity.Mvc注入DependencyResolver.SetResolver(新的UnityDependencyResolver(容器));需要解析身份类型和自定义WebApi注入config.DependencyResolver = new UnityResolver(容器);需要使用相同的UnityConfig注入WebApi控制器的容器. 更新3: 我改变了 app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<ApplicationUserManager>()); 对此 app.CreatePerOwinContext(() => UnityConfig.GetConfiguredContainer().Resolve<ApplicationUserManager>()); 在Startup.ConfigureAuth()中,现在我的WebApi容器完全正常工作并构建了所有必需的Identity类型,因此我可以禁用Mvc容器,甚至可以完全删除Unity.Mvc包. 感谢@Sam Farajpour Ghamari对许多事情的批判. 解决方法由于您使用UserManager与OWIN.您需要将OWIN容器与统一集成.我没有看到你的代码的其他部分特别是OWIN启动方法.我展示了一个简单的例子来演示如何通过自己来做到这一点.首先,如果您使用的是Entity Framework,则必须将您的上下文注册为unity.第二个注册其他类型的身份需要他们像这样: container.RegisterType<DbContext,MyDbContext>(new PerRequestLifetimeManager());
container.RegisterType<IUserStore<ApplicationUser>,UserStore<ApplicationUser>>(new PerRequestLifetimeManager());
container.RegisterType<ApplicationUserManager>(new PerRequestLifetimeManager());
container.RegisterType<IAuthenticationManager>(
new InjectionFactory(c => HttpContext.Current.GetOwinContext().Authentication))
container.RegisterType<ApplicationSignInManager>(new PerRequestLifetimeManager());
然后像这样更改ApplicationUserManager的构造函数: public ApplicationUserManager(IUserStore<ApplicationUser> store,IRepository repo)
: base(store)
{
this.Repository=repo;
// put your other configuration here instead of putting in
// static ApplicationUserManagerCreate() method.
this.UserValidator = new UserValidator<ApplicationUser>(this)
{
AllowOnlyAlphanumericUserNames = false,RequireUniqueEmail = true
};
// Configure validation logic for passwords
this.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,RequireNonLetterOrDigit = true,RequireDigit = true,RequireLowercase = true,RequireUppercase = true,};
// Configure user lockout defaults
this.UserLockoutEnabledByDefault = true;
this.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
this.MaxFailedAccessAttemptsBeforeLockout = 5;
// and your other configurations
}
现在在您的Startup.ConfigureAuth()方法中更改以下行: public void ConfigureAuth(IAppBuilder app)
{
app.CreatePerOwinContext(EFDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
// others
}
至: public void ConfigureAuth(IAppBuilder app)
{
app.CreatePerOwinContext(()=> DependencyResolver.Current.GetService<ApplicationUserManager>());
app.CreatePerOwinContext(()=> DependencyResolver.Current.GetService<ApplicationSignInManager>());
// other configs
}
此外,我们不再需要ApplicationUserManager Create()和ApplicationSignInManager.Create()方法,我们可以轻松删除它们,因为Unity现在负责创建我们的类. 现在我们将身份与统一完全融合.对于更复杂的信息,我非常鼓励您阅读this awesome blog post. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 命名Dom元素的Id属性的最佳实践
- asp.net-mvc-4 – 创建和编辑MVC4的相同视图
- ASP.Net错误:“应用程序池的身份无效”
- asp.net – 基于用户更改主题/ CSS
- asp.net-mvc – 有什么技巧/技巧使用亚音速与Asp.Net MVC框
- ASP.NET:Response.Redirect(…)后的代码会发生什么?
- 在对ASP.NET MVC Action的AJAX请求期间有网络请求超时时会发
- asp.net-mvc – ModelState.IsValid或Model.IsValid?
- asp.net-mvc – CssRewriteUrlTransform没有被调用
- asp.net-web-api – 具有Web Api RC的Ninject InSingletonS
- asp.net – System.Linq.Dynamic不支持OrderByDe
- asp.net-mvc-3 – ASP.Net MVC 3剃刀:部分定义但
- asp.net – 您何时会在Web自定义控件上使用Web用
- asp.net – 有没有办法禁用整个页面的事件验证?
- 是否有任何asp.net数据缓存支持缓存条目的背景填
- asp.net-mvc – ASP.NET MVC5/IIS Express无法调
- asp.net – System.Diagnostics.Process.Start不
- asp.net – NHibernate – ManagedWebSessionCon
- 使用ASP.Net和JSON格式化实现jQuery的jgGrid
- asp.net – 具有html5中的文本以外的输入类型的U
