Skip to content

Commit 53f4425

Browse files
authored
WinUI: Fix several problems with WinUI PKI security sample (#1485)
1 parent 836d24e commit 53f4425

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/WinUI/ArcGIS.WinUI.Viewer/Samples/Security/CertificateAuthenticationWithPki/CertificateAuthenticationWithPKI.xaml.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
88
// language governing permissions and limitations under the License.
99

10+
using CommunityToolkit.WinUI;
1011
using Esri.ArcGISRuntime.Portal;
1112
using Esri.ArcGISRuntime.Security;
1213
using Microsoft.UI.Xaml;
@@ -33,7 +34,17 @@ public CertificateAuthenticationWithPKI()
3334
InitializeComponent();
3435
}
3536

36-
public async Task<Credential> CreateCertCredentialAsync(CredentialRequestInfo info)
37+
private async Task<Credential> HandleCredentialRequest(CredentialRequestInfo info)
38+
{
39+
if (info.AuthenticationType == AuthenticationType.Certificate)
40+
{
41+
// ChallengeHandler may be called from a background thread, so we need to switch to the UI thread to show the dialog.
42+
return await DispatcherQueue.EnqueueAsync(async () => await CreateCertCredentialAsync(info));
43+
}
44+
return null;
45+
}
46+
47+
private async Task<Credential> CreateCertCredentialAsync(CredentialRequestInfo info)
3748
{
3849
// Handle challenges for a secured resource by prompting for a client certificate.
3950
Credential credential = null;
@@ -55,6 +66,7 @@ public async Task<Credential> CreateCertCredentialAsync(CredentialRequestInfo in
5566

5667
// Create a list view for rendering the list.
5768
ListView listview = new ListView();
69+
listview.SelectionMode = ListViewSelectionMode.Single;
5870

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

6880
// Display the dialog.
81+
dialog.XamlRoot = this.XamlRoot;
6982
await dialog.ShowAsync();
7083

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

103116
// Configure the authentication manager.
104-
AuthenticationManager.Current.ChallengeHandler = new ChallengeHandler(CreateCertCredentialAsync);
117+
AuthenticationManager.Current.ChallengeHandler = new ChallengeHandler(HandleCredentialRequest);
105118

106119
// Create the portal.
107120
ArcGISPortal portal = await ArcGISPortal.CreateAsync(new Uri(_serverUrl));

0 commit comments

Comments
 (0)