private async void CheckUriIdentity_Click(object sender, RoutedEventArgs e)
        {
            Uri    resourceUri;
            string resourceIdentity;
            ThreadNetworkContext context = null;

            try
            {
                if (!Uri.TryCreate(EnterpriseUri.Text.Trim(), UriKind.Absolute, out resourceUri))
                {
                    rootPage.NotifyUser("Invalid URI, please re-enter a valid URI", NotifyType.ErrorMessage);
                    return;
                }
                resourceIdentity =
                    await ProtectionPolicyManager.GetPrimaryManagedIdentityForNetworkEndpointAsync(
                        new HostName(resourceUri.Host));

                // if resourceIdentity is empty or Null, then it is considered personal

                if (!string.IsNullOrEmpty(resourceIdentity))
                {
                    context = ProtectionPolicyManager.CreateCurrentThreadNetworkContext(resourceIdentity);
                }

                // Access enterprise content

                rootPage.NotifyUser("Accessing Enterprise content.........", NotifyType.StatusMessage);

                HttpClientHandler httpHandler = new HttpClientHandler();
                if (!string.IsNullOrEmpty(UserNameBox.Text) &&
                    !string.IsNullOrEmpty(DomainBox.Text) &&
                    !string.IsNullOrEmpty(Passwordbox.Password))
                {
                    httpHandler.Credentials = new NetworkCredential(UserNameBox.Text, Passwordbox.Password, DomainBox.Text);
                }
                httpHandler.AllowAutoRedirect = true;

                using (var client = new HttpClient(httpHandler))
                {
                    var message = new HttpRequestMessage(HttpMethod.Get, resourceUri);
                    message.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1)");
                    var result = await client.SendAsync(message);
                }

                // Access to network resource was a success. If it wasn't, exception would have been thrown

                rootPage.NotifyUser("Successfully got network resource", NotifyType.StatusMessage);
            }
            catch (Exception ex)
            {
                rootPage.NotifyUser(ex.ToString(), NotifyType.ErrorMessage);
            }
            finally
            {
                if (context != null)
                {
                    context.Dispose();
                }
            }
        }
示例#2
0
        private async void ConnectToUri()
        {
            Uri resourceUri;

            if (!Uri.TryCreate(ResourceUriText.Text, UriKind.Absolute, out resourceUri))
            {
                rootPage.NotifyUser("Invalid URI", NotifyType.ErrorMessage);
                return;
            }

            var hostName = new HostName(resourceUri.Host);

            try
            {
                string resourceIdentity = await ProtectionPolicyManager.GetPrimaryManagedIdentityForNetworkEndpointAsync(hostName);

                // if there is no resourceIdentity, then the resource is considered personal, and no thread network context is needed.
                // Otherwise, it requires the enterprise context.
                DisplayNetworkIdentity(resourceIdentity);

                rootPage.NotifyUser("Accessing network resource...", NotifyType.StatusMessage);

                if (UseSystemNetHttpClient.IsChecked.Value)
                {
                    ConnectUsingNetHttpClient(resourceIdentity, resourceUri);
                }
                else if (UseWindowsWebHttpClient.IsChecked.Value)
                {
                    ConnectUsingWebHttpClient(resourceIdentity, resourceUri);
                }
                else if (UseBackgroundTransferButton.IsChecked.Value)
                {
                    ConnectUsingBackgroundTransfer(resourceIdentity, resourceUri);
                }
                else
                {
                    rootPage.NotifyUser("Please select a method to connect.", NotifyType.ErrorMessage);
                }
            }
            catch (Exception ex)
            {
                // Access to the network resource was not successful.
                rootPage.NotifyUser(ex.ToString(), NotifyType.ErrorMessage);
            }
        }
示例#3
0
        private async void CheckUriIdentity_Click(object sender, RoutedEventArgs e)
        {
            Uri    resourceUri;
            string resourceIdentity;

            try
            {
                if (!Uri.TryCreate(ResourceUriText.Text.Trim(), UriKind.Absolute, out resourceUri))
                {
                    rootPage.NotifyUser("Invalid URI, please re-enter a valid URI", NotifyType.ErrorMessage);
                    return;
                }
                resourceIdentity = await ProtectionPolicyManager.GetPrimaryManagedIdentityForNetworkEndpointAsync(
                    new HostName(resourceUri.Host));

                DisplayNetworkIdentity(resourceIdentity);
            }
            catch (Exception ex)
            {
                rootPage.NotifyUser(ex.ToString(), NotifyType.ErrorMessage);
            }
        }
        private async void CheckUriIdentity_Click(object sender, RoutedEventArgs e)
        {
            Uri    resourceUri;
            string resourceIdentity;
            ThreadNetworkContext context = null;

            IdentityBox.Text = String.Empty;

            try
            {
                rootPage.NotifyUser("Creating file.........", NotifyType.StatusMessage);

                StorageFile resultFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
                    "ResultFile.txt",
                    CreationCollisionOption.GenerateUniqueName);

                if (!Uri.TryCreate(EnterpriseUri.Text.Trim(), UriKind.Absolute, out resourceUri))
                {
                    rootPage.NotifyUser("Invalid URI, please re-enter a valid URI", NotifyType.ErrorMessage);
                    return;
                }
                resourceIdentity = await ProtectionPolicyManager.GetPrimaryManagedIdentityForNetworkEndpointAsync(
                    new HostName(resourceUri.Host));

                // if resourceIdentity is empty or Null, then it is considered personal
                IdentityBox.Text = resourceIdentity;

                if (!string.IsNullOrEmpty(resourceIdentity))
                {
                    context = ProtectionPolicyManager.CreateCurrentThreadNetworkContext(resourceIdentity);
                }

                // Access enterprise content

                rootPage.NotifyUser("Accessing network resource.........", NotifyType.StatusMessage);

                BackgroundDownloader downloader = new BackgroundDownloader();

                if (!string.IsNullOrEmpty(UserNameBox.Text) &&
                    !string.IsNullOrEmpty(Passwordbox.Password))
                {
                    downloader.ServerCredential = new PasswordCredential(
                        resourceUri.AbsoluteUri,
                        UserNameBox.Text,
                        Passwordbox.Password);
                }

                DownloadOperation download = downloader.CreateDownload(resourceUri, resultFile);

                await HandleDownloadAsync(download);
            }
            catch (Exception ex)
            {
                rootPage.NotifyUser(ex.ToString(), NotifyType.ErrorMessage);
            }
            finally
            {
                if (context != null)
                {
                    context.Dispose();
                }
            }
        }