Encrypt messages sent externally
I have modified the previous GetXmlDocument method so that it can use the X.509-based asymmetric encryption technology implemented by WSE. The FindCertificateBySubjectString method can be used to receive public backups of client certificates, a client certificate from the personal storage room of the Local Machine Account. This certificate is then used to create a new X.509 Security Token, which will be added to the SoapContext Security Token set of the Response Message. In addition, for the namespace referenced in the symmetric encryption example, you should add a using directive to reference a Microsoft. WebServices. Security. X509 namespace. The code for the GetXmlDocument method is as follows:
// Create a simple XML document for returning
XmlDocument myDoc = new XmlDocument ();
MyDoc. InnerXml =
"<EncryptedResponse> This is sensitive data. </EncryptedResponse> ";
"<EncryptedResponse> sensitive data. </EncryptedResponse> ";
// Obtain the SoapContext of the Response Message
SoapContext myContext = HttpSoapContext. ResponseContext;
// Open and read the personal certificate storage room of the Local Machine Account
X509CertificateStore myStore =
X509CertificateStore. LocalMachineStore (
X509CertificateStore. MyStore );
MyStore. OpenRead ();
// Search for all certificates named "my certificates" and add all matching certificates to the certificate set
X509CertificateCollection myCerts =
MyStore. FindCertificateBySubjectString ("My Certificate ");
X509Certificate myCert = null;
// Search for the first certificate in the Set
If (myCerts. Count> 0)
{
MyCert = myCerts [0];
}
// Confirm that we have a certificate that can be used for encryption
If (myCert = null |! MyCert. SupportsDataEncryption)
{
Throw new ApplicationException ("Service is not able