0

Does anyone know how we can inject context into User Manager > MongoDB serStore at runtime in .net core 2.0.

We cannot do this at startup due to the context being dynamic but the UserStore is not accessible and UserManager has too many variables to new up, and it is wrong. Are there any solutions?

public class UserStore<TUser> :
        IUserPasswordStore<TUser>,
        IUserRoleStore<TUser>,
        IUserLoginStore<TUser>,
        IUserSecurityStampStore<TUser>,
        IUserEmailStore<TUser>,
        IUserClaimStore<TUser>,
        IUserPhoneNumberStore<TUser>,
        IUserTwoFactorStore<TUser>,
        IUserLockoutStore<TUser>,
        IQueryableUserStore<TUser>,
        IUserAuthenticationTokenStore<TUser>
    where TUser : IdentityUser
{
    private readonly IMongoCollection<TUser> _Users;

//THIS IS WHERE WE WANT TO INJECT THE users AT RUNTIME

    public UserStore(IMongoCollection<TUser> users)
    {
        _Users = users;
    }

    public virtual void Dispose()
    {
        // no need to dispose of anything, mongodb handles connection pooling automatically
    }

    public virtual async Task<IdentityResult> CreateAsync(TUser user, CancellationToken token)
    {
        await _Users.InsertOneAsync(user, cancellationToken: token);
        return IdentityResult.Success;
    }

unfortunately users is null at startup, and should be as the tenant has not been created at that point.

We have also been using the saaskit.Multitenancy and just can't find a solution.

Any help would be much appreciated.

Thanks

Dom
  • 1
  • 2

1 Answers1

0

i think u need a generic repository to act as a wrapper for IMongoCollection then inject the repository inside controllers

 public class Repository<T>
{
    public IMongoCollection<T> Collection { get; private set; }

    public Repository(IDbFactory dbFactory)
    {
        MongoClient client = new MongoClient("ur connection string");

        this.Collection = client.GetDatabase("db").GetCollection<T>(typeof(T).Name);
    }



       public T Find(Expression<Func<T, bool>> filter)
    {
        return this.Collection.AsQueryable<T>().FirstOrDefault<T>(filter);
    }

    public async Task<T> FindAsync(Expression<Func<T, bool>> filter)
    {
        return await this.Collection.AsQueryable<T>().FirstOrDefaultAsync<T>(filter);
    }

            // here add more methods
}

then register the dependency as below inside Startup.cs

 public void ConfigureServices(IServiceCollection services)
    {          
        services.AddTransient(typeof(IRepository<>), typeof(Repository<>));

        services.AddMvc();
    }

finally inside controllers u inject the generic repository, also dont forget to Implement the IDisopsible in genereic repository

 public class ProductController : Controller
{
    private readonly IRepository<Product> _productRepository = null;

    public ProductController(IRepository<Product> productRepository)
    {
        this._productRepository = productRepository;
    }
}
Behnam Abdy
  • 345
  • 5
  • 10