private async Task<ApplicationUser> addAsync(ApplicationUser user, string password)
        {
            var result = await _userManager.CreateAsync(user, password);

            if (result.Succeeded)
                return user;
            else
                return null;
        }
        public async Task<bool> AddTenantManagerAsync(ApplicationUser user, string password)
        {
            var newUser = await addAsync(user, password);
            if (newUser != null)
                await SetTenantManagerRole(newUser);
            else
                return false;

            return true;
        }
        public async Task<bool> AddAsync(Tenant tenant, ApplicationUser creator)
        {
            tenant.Creator = creator.UserName;

            _context.Tenants.Add(tenant);
            await _context.SaveChangesAsync();

            _myHealthDataInitializer.CreateTenantSampleData(_serviceProvider, tenant.TenantId);

            return true;
        }
        public async Task<IEnumerable<Tenant>> GetByCreator(ApplicationUser creator, int pageSize, int pageCount)
        {
            var tenants = await _context.Tenants
                .Where(t => t.Creator == creator.UserName)
                .OrderByDescending(t => t.TenantId)
                .Skip(pageSize * pageCount)
                .Take(pageSize)
                .Select(t => new Tenant()
                {
                    TenantId = t.TenantId,
                    Address = t.Address,
                    AssociatedUsername = t.AssociatedUsername,
                    City = t.City,
                    Creator = t.Creator,
                    WaitTimeAvg = t.WaitTimeAvg,
                    Name = t.Name,
                    // patientId is calculated each time due to it could change in any moment.
                    PatientId = _context.Patients.Where(p => p.TenantId == t.TenantId).Min(p => p.PatientId)
                })
                .ToListAsync();

            return tenants;
        }
        private static async Task CreateDefaultUser(IServiceProvider serviceProvider, int tenantId)
        {
            var userManager = serviceProvider.GetService<UserManager<ApplicationUser>>();
            var appEnv = serviceProvider.GetService<IApplicationEnvironment>();

            var builder = new ConfigurationBuilder()
                    .SetBasePath(appEnv.ApplicationBasePath)
                    .AddJsonFile("appsettings.json");

            var configuration = builder.Build();

            var user = await userManager.FindByNameAsync(configuration["DefaultUsername"]);
            if (user == null)
            {
                user = new ApplicationUser {
                    UserName = configuration["DefaultUsername"],
                    TenantId = tenantId,
                    Picture = GetDefaultUser()
                };
                await userManager.CreateAsync(user, configuration["DefaultPassword"]);
                await userManager.AddClaimAsync(user, new Claim("ManageStore", "Allowed"));
            }
        }
 private static async Task CreateDefaultAdmin()
 {
     var superadmin = await _userManager.FindByNameAsync(_configuration["DefaultAdminUsername"]);
     if (superadmin == null)
     {
         superadmin = new ApplicationUser
         {
             UserName = _configuration["DefaultAdminUsername"]
         };
         await _userManager.CreateAsync(superadmin, _configuration["DefaultAdminPassword"]);
         await _userManager.AddClaimAsync(superadmin, new Claim("ManageUsers", "Allowed"));
         await _userManager.AddClaimAsync(superadmin, new Claim("ManageTenants", "Allowed"));
     }
 }
 private static async Task CreateDefaultUser(int tenantId)
 {
     var user = await _userManager.FindByNameAsync(_configuration["DefaultUsername"]);
     if (user == null)
     {
         user = new ApplicationUser {
             UserName = _configuration["DefaultUsername"],
             TenantId = tenantId
         };
         await _userManager.CreateAsync(user, _configuration["DefaultPassword"]);
         await _userManager.AddClaimAsync(user, new Claim("ManageUsers", "Denied"));
         await _userManager.AddClaimAsync(user, new Claim("ManageTenants", "Denied"));
     }
 }
 private async Task<bool> AddAssociatedUserAsync(Tenant tenant, string password)
 {
     var associatedUser = new ApplicationUser { UserName = tenant.AssociatedUsername };
     associatedUser.TenantId = tenant.TenantId;
     return await _ApplicationUsersRepository.AddAsync(associatedUser, password);
 }
 public async Task<bool> ValidatePasswordAsync(ApplicationUser user, string password)
 {
     return (await _passwordValidator.ValidateAsync(_userManager, user, password)).Succeeded;
 }
        public async Task<bool> UpdateAsync(ApplicationUser user)
        {
            var existingUser = await GetByIdAsync(user.Id);

            existingUser.FirstName = user.FirstName;
            existingUser.LastName = user.LastName;
            existingUser.Email = user.Email;
            existingUser.UserName = user.UserName;

            var updateResult = await _userManager.UpdateAsync(existingUser);

            return updateResult.Succeeded;
        }
 public async Task<bool> SetTenantManagerRole(ApplicationUser user)
 {
     var result = await _userManager.AddClaimAsync(user, new Claim("ManageTenants", "Allowed"));
     return result.Succeeded;
 }
 public async Task<bool> AddAsync(ApplicationUser user, string password)
 {
     var newUser = await addAsync(user, password);
     return newUser != null;
 }
        private ApplicationUser Build(ApplicationUser user)
        {
            var _user = new ApplicationUser { };
            _user.Id = user.Id;
            _user.FirstName = user.FirstName;
            _user.LastName = user.LastName;
            _user.Email = user.Email;
            _user.UserName = user.UserName;
            _user.TenantId = user.TenantId;

            return _user;
        }
        public async Task<bool> ChangePassword(ApplicationUser user, string password)
        {
            await _userManager.RemovePasswordAsync(user);
            var result = await _userManager.AddPasswordAsync(user, password);

            return result.Succeeded;
        }