private void SetupLocalStore()
        {
            var serviceInstance = new HealthServiceInstance
            {
                Id               = "1",
                Name             = "US",
                Description      = "US instance",
                HealthServiceUrl = new Uri("https://platform.healthvault-ppe.com/platform/wildcat.ashx"),
                ShellUrl         = new Uri("https://account.healthvault-ppe.com/")
            };

            var applicationCreationInfo = new ApplicationCreationInfo
            {
                AppInstanceId    = new Guid(ApplicationInstanceId),
                SharedSecret     = ApplicationSharedSecret,
                AppCreationToken = ApplicationCreationToken
            };

            var sessionCredential = new SessionCredential
            {
                Token         = SessionToken,
                SharedSecret  = SessionSharedSecret,
                ExpirationUtc = DateTimeOffset.UtcNow.AddHours(4)
            };

            var personInfo = new PersonInfo
            {
                PersonId = PersonId
            };

            _subLocalObjectStore
            .ReadAsync <HealthServiceInstance>(HealthVaultSodaConnection.ServiceInstanceKey)
            .Returns(serviceInstance);

            _subLocalObjectStore
            .ReadAsync <ApplicationCreationInfo>(HealthVaultSodaConnection.ApplicationCreationInfoKey)
            .Returns(applicationCreationInfo);

            _subLocalObjectStore
            .ReadAsync <SessionCredential>(HealthVaultSodaConnection.SessionCredentialKey)
            .Returns(sessionCredential);

            _subLocalObjectStore
            .ReadAsync <PersonInfo>(HealthVaultSodaConnection.PersonInfoKey)
            .Returns(personInfo);
        }
        private async Task ProvisionForSodaAuthAsync()
        {
            // Set a temporary service instance for the NewApplicationCreationInfo and GetServiceDefinition calls.
            var defaultHealthVaultUrl      = Configuration.DefaultHealthVaultUrl;
            var defaultHealthVaultShellUrl = Configuration.DefaultHealthVaultShellUrl;
            var masterApplicationId        = Configuration.MasterApplicationId;

            ServiceInstance = new HealthServiceInstance(
                "1",
                "Default",
                "Default HealthVault instance",
                UrlUtilities.GetFullPlatformUrl(defaultHealthVaultUrl),
                defaultHealthVaultShellUrl);

            // Note: This apparent circular call is intentional. This method is called from AuthenticateAsync.
            // PlatformClient is calling HealthVaultConnectionBase.ExecuteAsync("NewApplicationCreationInfo"),
            // which avoids calling AuthenticateAsync because "NewApplicationCreationInfo" is an anonymous method.
            IPlatformClient         platformClient             = CreatePlatformClient();
            ApplicationCreationInfo newApplicationCreationInfo = await platformClient.NewApplicationCreationInfoAsync().ConfigureAwait(false);

            string environmentInstanceId = await _shellAuthService.ProvisionApplicationAsync(
                defaultHealthVaultShellUrl,
                masterApplicationId,
                newApplicationCreationInfo.AppCreationToken,
                newApplicationCreationInfo.AppInstanceId.ToString()).ConfigureAwait(false);

            ServiceInfo serviceInfo = await platformClient.GetServiceDefinitionAsync(ServiceInfoSections.Topology).ConfigureAwait(false);

            HealthServiceInstance bouncedHealthServiceInstance;

            if (!serviceInfo.ServiceInstances.TryGetValue(environmentInstanceId, out bouncedHealthServiceInstance))
            {
                // TODO: Come up with better error for  Current HealthServiceException is restrictive.
                throw new HealthServiceException(HealthServiceStatusCode.Failed);
            }

            // We've successfully made it through the flow. Save all the information.
            await _localObjectStore.WriteAsync(ServiceInstanceKey, bouncedHealthServiceInstance).ConfigureAwait(false);

            ServiceInstance = bouncedHealthServiceInstance;

            await _localObjectStore.WriteAsync(ApplicationCreationInfoKey, newApplicationCreationInfo).ConfigureAwait(false);

            ApplicationCreationInfo = newApplicationCreationInfo;
        }
        private async Task ReadPropertiesFromLocalStorageAsync()
        {
            if (ServiceInstance == null)
            {
                ServiceInstance = await _localObjectStore.ReadAsync <HealthServiceInstance>(ServiceInstanceKey).ConfigureAwait(false);
            }

            if (ApplicationCreationInfo == null)
            {
                ApplicationCreationInfo = await _localObjectStore.ReadAsync <ApplicationCreationInfo>(ApplicationCreationInfoKey).ConfigureAwait(false);
            }

            if (SessionCredential == null)
            {
                SessionCredential = await _localObjectStore.ReadAsync <SessionCredential>(SessionCredentialKey).ConfigureAwait(false);
            }

            if (_personInfo == null)
            {
                _personInfo = await _localObjectStore.ReadAsync <PersonInfo>(PersonInfoKey).ConfigureAwait(false);
            }
        }
        public async Task DeauthorizeApplicationAsync()
        {
            using (await _authenticateLock.LockAsync().ConfigureAwait(false))
            {
                // Delete session data from store to ensure we will always be in a clean state
                await _localObjectStore.DeleteAsync(ServiceInstanceKey).ConfigureAwait(false);

                await _localObjectStore.DeleteAsync(ApplicationCreationInfoKey).ConfigureAwait(false);

                await _localObjectStore.DeleteAsync(SessionCredentialKey).ConfigureAwait(false);

                await _localObjectStore.DeleteAsync(PersonInfoKey).ConfigureAwait(false);

                if (ServiceInstance != null &&
                    ApplicationCreationInfo != null &&
                    SessionCredential != null &&
                    _personInfo != null)
                {
                    var platformClient = CreatePlatformClient();

                    foreach (HealthRecordInfo record in _personInfo.AuthorizedRecords.Values)
                    {
                        try
                        {
                            await platformClient.RemoveApplicationRecordAuthorizationAsync(record.Id).ConfigureAwait(false);
                        }
                        catch (Exception)
                        {
                            // Ignore, this is a non-essential cleanup step
                        }
                    }
                }

                ServiceInstance         = null;
                ApplicationCreationInfo = null;
                SessionCredential       = null;
                _personInfo             = null;
            }
        }
        public async Task <ApplicationCreationInfo> NewApplicationCreationInfoAsync()
        {
            HealthServiceResponseData responseData = await _connection.ExecuteAsync(HealthVaultMethods.NewApplicationCreationInfo, 1).ConfigureAwait(false);

            return(ApplicationCreationInfo.Create(responseData.InfoNavigator));
        }