public AdalTokenCache(string userName)
        {
            // associate the cache to the current user of the web app
            _userName = userName;
            _userAccountService = new UserAccountService();

            AfterAccess = AfterAccessNotification;
            BeforeAccess = BeforeAccessNotification;
            BeforeWrite = BeforeWriteNotification;

            // look up the entry in the DB
            _userAccount = _userAccountService.FetchByUsername(_userName);

            // place the entry in memory
            Deserialize((_userAccount == null) ? null : _userAccount.CachedData);
        }
        public ActionResult Index()
        {
            // Fetch required data
            var accountService = new UserAccountService();
            var user = accountService.FetchByUsername(HttpContext.User.Identity.SplitName());

            var tenantService = new TenantService();
            var tenants = tenantService.FetchTenants();

            // Build the tenants viewmodel
            var viewModel = tenants != null
                ? new MyAccountViewModel()
                {
                    UserAccounts = tenants.GroupBy(u => u.Username).Select(u => new UserAccountViewModel()
                    {
                        Username = u.Key,
                        Tenants = u.Select(t => new TenantViewModel()
                        {
                            Id = t.TenantId,
                            Plan = t.ProvisioningOption,
                            Theme = t.Theme,
                            Status = t.AzureServicesProvisioned ? ProvisioningStatus.Deployed : ProvisioningStatus.NotDeployed,
                            AzureServicesProvisioned = t.AzureServicesProvisioned,
                        }).ToList()
                    }).ToList()
                }
                : new MyAccountViewModel();

            // Get working values
            var pipelineRunning = TempData["PipelineRunning"] != null && (bool)TempData["PipelineRunning"];
            var username = TempData["Username"] != null ? (string)TempData["Username"] : string.Empty;

            // Add each tenants services
            foreach (var userAccount in viewModel.UserAccounts)
            { 
                foreach (var company in userAccount.Tenants)
                {
                    var provisioningService = ProvisioningService.CreateForAdmin(company.Id);

                    company.Services = provisioningService.GetStatusses().Select(s => new ServiceStatusViewModel()
                    {
                        StatusId = s.Id,
                        Name = s.Name,
                        Service = s.Service,
                        Status = company.AzureServicesProvisioned ? ProvisioningStatus.Deployed : ProvisioningStatus.NotDeployed
                    }).ToList();
                }

                // Set already provisioned tenant services to active
                userAccount.Tenants.Where(c => c.AzureServicesProvisioned)
                           .ForEach(c => c.Services.ForEach(s => s.Status = ProvisioningStatus.Deployed));

                // Update statusses
                foreach (var tenant in userAccount.Tenants)
                {
                    // Set the Status
                    if (!tenant.AzureServicesProvisioned)
                    {
                        tenant.Status = pipelineRunning && userAccount.Username.Equals(username) ? ProvisioningStatus.Removing : ProvisioningStatus.NotDeployed;
                    }
                    else
                    {
                        tenant.Status = pipelineRunning && userAccount.Username.Equals(username) ? ProvisioningStatus.Removing : ProvisioningStatus.Deployed;
                    }
                }
            }

            // Build up the ViewBag
            ViewBag.PipelineRunning = pipelineRunning;
            ViewBag.Username = username;

            return View(viewModel);
        }