在调用asp.net身份usermanager.CreateAsync
方法时,温莎城堡容器将asp.net mvc控制器放置
UserManager.CreateAsync
方法首先调用FindByNameAsync
,该方法将返回null
,并且CreateAsync
中预期的UserStore
不会被调用,因为mvc控制器是由Windsor DI处理的容器。
任何帮助将不胜感激。
代码:
// custom implementation of UserStore
public class UserStore : IUserStore<ApplicationUser,int>
{
#region IUserStore
// create a new user
public Task CreateAsync(ApplicationUser user)
{
if (user != null)
{
return Task.Factory.StartNew(() =>
{
// UserSerivce.NewUser(user); // TODO
});
}
throw new ArgumentNullException("user");
}
}
// controller method
[HttpPost]
public async Task<ActionResult> Register(RegisterModel model)
{
if (ModelState.IsValid)
{
//model.Password need to be hashed
var user = new ApplicationUser { UserName = model.Email, Email = model.Email, PasswordHash = model.Password}; // populate other data
var result = await UserManager.CreateAsync(user);// calls FindByNameAsync first and then not calling CreateAsync as Controller instance is disposed.
if (result.Succeeded)
{
}
}
}
public class UserStore : IUserStore<ApplicationUser,int>
{
#region IUserStore
// does not get called as controller disposed before calling this.
// create a new user
public Task CreateAsync(ApplicationUser user)
{
if (user != null)
{
return Task.Factory.StartNew(() =>
{
// use User Service in services folder.
// UserController.NewUser(user); // TODO
});
}
throw new ArgumentNullException("user");
}
// gets called ok.
public Task<ApplicationUser> FindByNameAsync(string userName)
{
if (!string.IsNullOrEmpty(userName))
{
// check with db
return null; // for testing
}
throw new ArgumentNullException("userName");
}
}
// windsor DI container
public class MvcControllersInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Classes.FromThisAssembly()
.BasedOn<IController>()
.LifestylePerWebRequest());
}
}