Skip to content

Commit

Permalink
WinUI: Fix several problems with WinUI PKI security sample (#1485)
Browse files Browse the repository at this point in the history
  • Loading branch information
mstefarov authored Jul 22, 2024
1 parent 836d24e commit 53f4425
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.

using CommunityToolkit.WinUI;
using Esri.ArcGISRuntime.Portal;
using Esri.ArcGISRuntime.Security;
using Microsoft.UI.Xaml;
Expand All @@ -33,7 +34,17 @@ public CertificateAuthenticationWithPKI()
InitializeComponent();
}

public async Task<Credential> CreateCertCredentialAsync(CredentialRequestInfo info)
private async Task<Credential> HandleCredentialRequest(CredentialRequestInfo info)
{
if (info.AuthenticationType == AuthenticationType.Certificate)
{
// ChallengeHandler may be called from a background thread, so we need to switch to the UI thread to show the dialog.
return await DispatcherQueue.EnqueueAsync(async () => await CreateCertCredentialAsync(info));
}
return null;
}

private async Task<Credential> CreateCertCredentialAsync(CredentialRequestInfo info)
{
// Handle challenges for a secured resource by prompting for a client certificate.
Credential credential = null;
Expand All @@ -55,6 +66,7 @@ public async Task<Credential> CreateCertCredentialAsync(CredentialRequestInfo in

// Create a list view for rendering the list.
ListView listview = new ListView();
listview.SelectionMode = ListViewSelectionMode.Single;

// Use a template defined as a resource in XAML.
listview.ItemTemplate = (DataTemplate)this.Resources["CertificateTemplate"];
Expand All @@ -66,6 +78,7 @@ public async Task<Credential> CreateCertCredentialAsync(CredentialRequestInfo in
dialog.Content = listview;

// Display the dialog.
dialog.XamlRoot = this.XamlRoot;
await dialog.ShowAsync();

// Make sure the user chose a certificate.
Expand Down Expand Up @@ -101,7 +114,7 @@ private async void Button_Click(object sender, RoutedEventArgs e)
_serverUrl = PortalUrlTextbox.Text;

// Configure the authentication manager.
AuthenticationManager.Current.ChallengeHandler = new ChallengeHandler(CreateCertCredentialAsync);
AuthenticationManager.Current.ChallengeHandler = new ChallengeHandler(HandleCredentialRequest);

// Create the portal.
ArcGISPortal portal = await ArcGISPortal.CreateAsync(new Uri(_serverUrl));
Expand Down

0 comments on commit 53f4425

Please sign in to comment.