asp.net identity 2.0 unity not resolving default user store

前端 未结 2 486

i get the following exception when trying to configure Unity using Unity.Mvc5 with an MVC 5 application using Identity 2.0 and the Identity 2.0 Samples boilerplate. i have read

2条回答
  •  终归单人心
    2021-02-02 02:34

    ok i figured it out. i think. i created my own userstore class and plugged it in. this was helpful somewhat. http://odetocode.com/blogs/scott/archive/2014/01/20/implementing-asp-net-identity.aspx

    unity config

    container.RegisterType(new HierarchicalLifetimeManager());
    container.RegisterType, bcUserStore>(new HierarchicalLifetimeManager());
    

    user manager

    public static ApplicationUserManager Create(IdentityFactoryOptions options, IOwinContext context)
    {
       var manager = new ApplicationUserManager(new bcUserStore(context.Get()));
    // other code that's not relevant
    }
    

    user store

    public class bcUserStore : IUserStore
    {
        private IDbSet _users;
        private ApplicationDbContext _context;
        public bcUserStore(ApplicationDbContext context)
        {
            _users = context.Users;
            _context = context;
        }
        public System.Threading.Tasks.Task CreateAsync(ApplicationUser user)
        {
            user.Id = Guid.NewGuid().ToString();
            _users.Add(user);
            return _context.SaveChangesAsync();
        }
    
        public System.Threading.Tasks.Task DeleteAsync(ApplicationUser user)
        {
            _users.Remove(user);
            return _context.SaveChangesAsync();
        }
    
        public System.Threading.Tasks.Task FindByIdAsync(string userId)
        {
            return _users.FirstOrDefaultAsync(x => x.Id == userId);
        }
    
        public System.Threading.Tasks.Task FindByNameAsync(string userName)
        {
            return _users.FirstOrDefaultAsync(x => x.UserName == userName);
        }
    
        public System.Threading.Tasks.Task UpdateAsync(ApplicationUser user)
        {
            var current = _users.Find(user.Id);
            _context.Entry(current).CurrentValues.SetValues(user);
            return _context.SaveChangesAsync();
        }
    
        public void Dispose()
        {
            _users = null;
            _context.Dispose();
        }
    }
    

提交回复
热议问题