private void AfterAccessNotification(TokenCacheNotificationArgs args)
        {
            // if state changed
            if (HasStateChanged)
            {
                // check for an existing entry
                _userAccount = _userAccountService.FetchByUsername(_userName);

                if (_userAccount == null)
                {
                    // Create the account
                    _userAccountService.Create(new CreateUserAccountModel()
                    {
                        Firstname = "",
                        Lastname = "",
                        Username = _userName,
                        CachedData = Serialize(),
                        UpdateDate = DateTime.Now
                    });
                }
                else
                {
                    // Update the account
                    _userAccount.CachedData = this.Serialize();
                    _userAccount.UpdateDate = DateTime.Now;

                    _userAccountService.UpdateCacheData(_userAccount);
                }

                HasStateChanged = false;
            }
        }
        public UserAccountModel Fetch(string username)
        {
            // Build up the parameters
            var parameters = new List<SqlParameter>()
            {
                CreateParameter("@Username", SqlDbType.VarChar, username)
            };

            // Run command
            var dataSet = FetchFilteredData("Sp_Fetch_UserAccount", parameters);

            // Check if any data
            if (dataSet.Tables.Count <= 0 || dataSet.Tables[0].Rows.Count <= 0)
            {
                return null;
            }

            // Return the first row
            var user = dataSet.Tables[0].Rows[0];
            var domainModel = new UserAccountModel()
            {
                UserAccountId = Cast<int>(user["UserAccountId"]),
                FirstName = user["Firstname"].ToString(),
                LastName = user["Lastname"].ToString(),
                Username = user["Username"].ToString(),
                Password = user["Password"].ToString(),
                CachedData = Cast<byte[]>(user["CachedData"]),
                UpdateDate = Cast<DateTime>(user["UpdateDate"]),
            };

            return domainModel;
        }
        public void UpdateCacheData(UserAccountModel domainModel)
        {
            // Create repositories
            var userAccountRepository = new UserAccountRepository();

            // Create the UserAccount
            userAccountRepository.UpdateCacheData(domainModel);
        }
        public int UpdateCacheData(UserAccountModel domainModel)
        {
            // Build up the parameters
            var parameters = new List<SqlParameter>()
            {
                CreateParameter("@Username", SqlDbType.VarChar, domainModel.Username),
                CreateParameter("@CachedData", SqlDbType.VarBinary, domainModel.CachedData),
                CreateParameter("@UpdateDate", SqlDbType.DateTime, domainModel.UpdateDate)
            };

            // Run command
            return InsertData("Sp_Update_UserAccount_CacheData", parameters);
        }
        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);
        }
        private void BeforeAccessNotification(TokenCacheNotificationArgs args)
        {
            var account = _userAccountService.FetchByUsername(_userName);

            if (_userAccount == null)
            {
                // first time access
                _userAccount = account;
            }
            else
            {
                // if the in-memory copy is older than the persistent copy
                if (account != null && account.UpdateDate > _userAccount.UpdateDate)
                {
                    // read from from storage, update in-memory copy
                    _userAccount = account;
                }
            }

            Deserialize((_userAccount == null) ? null : _userAccount.CachedData);
        }