/// <summary> /// Prompts the user for a certificate to authenticate that user on the channel. /// </summary> /// <param name="state">A generic parameter used to initialize the thread.</param> static void PromptForCertificate(CredentialAsyncResult credentialAsyncResult) { // This will select a list of valid certificates from the store that can be used for client authentication. X509Store store = new X509Store(CertificateChannelInitializer.myStoreName, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); X509Certificate2Collection clientCertificates = store.Certificates.Find( X509FindType.FindByApplicationPolicy, CertificateChannelInitializer.oidClientAuthentication.Value, true); // This dialog will prompt the user with a list of certificate that match the WindowCertificate windowCertificate = new WindowCertificate(); foreach (X509Certificate2 x509Certificate2 in clientCertificates) { windowCertificate.X509Certificate2s.Add(x509Certificate2); } windowCertificate.X509Certificate2 = credentialAsyncResult.Credentials as X509Certificate2; if (windowCertificate.ShowDialog() == true) { credentialAsyncResult.IsCanceled = false; credentialAsyncResult.Credentials = windowCertificate.X509Certificate2; } else { credentialAsyncResult.IsCanceled = true; } // If a valid callback was provided in the IAsyncResult structure then send a signal that we're through. if (credentialAsyncResult.AsyncCallback != null) { credentialAsyncResult.AsyncCallback(credentialAsyncResult); } }
/// <summary> /// Prompts the user for a certificate to authenticate that user on the channel. /// </summary> /// <param name="credentialAsyncResult">The CredentialAsyncResult that is called to complete the initialization.</param> static void PromptForUserName(CredentialAsyncResult credentialAsyncResult) { // This dialog will prompt the user with a list of certificate that match the WindowDistinguishedName windowBasic = new WindowDistinguishedName(); windowBasic.DistinguishedNameCredential = credentialAsyncResult.Credentials as DistinguishedNameCredential; windowBasic.ServerName = credentialAsyncResult.IClientChannel.RemoteAddress.Uri.Host; // This will display the dialog and wait for the user to either accept the credentials or cancel out of the login. if (windowBasic.ShowDialog() == true) { credentialAsyncResult.IsCanceled = false; credentialAsyncResult.Credentials = windowBasic.DistinguishedNameCredential; } else { credentialAsyncResult.IsCanceled = true; } // If a valid callback was provided in the IAsyncResult structure then send a signal that we're through. if (credentialAsyncResult.AsyncCallback != null) { credentialAsyncResult.AsyncCallback(credentialAsyncResult); } }