处理ASP.NET标识Usermanager.CreateAsync方法时配置的ASP.NET mvc控制器

时间:2018-11-16 11:09:25

标签: asp.net-mvc asp.net-identity

在调用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());            
    }
}       

0 个答案:

没有答案