PowerTCP Mail for .NET
CertificateReceived Event
See Also  Example Send comments on this topic.
Dart.PowerTCP.Mail Namespace > Tcp Class : CertificateReceived Event




This event is raised when a certificate has been received to be authenticated.

Syntax

Visual Basic (Declaration) 
<CategoryAttribute("Security")>
<DescriptionAttribute("Raised when a remote certificate has been received for verification.")>
Public Event CertificateReceived As CertificateReceivedEventHandler
Visual Basic (Usage)Copy Code
Dim instance As Tcp
Dim handler As CertificateReceivedEventHandler
 
AddHandler instance.CertificateReceived, handler
C# 
[CategoryAttribute("Security")]
[DescriptionAttribute("Raised when a remote certificate has been received for verification.")]
public event CertificateReceivedEventHandler CertificateReceived
Managed Extensions for C++ 
[CategoryAttribute("Security")]
[DescriptionAttribute("Raised when a remote certificate has been received for verification.")]
public: __event CertificateReceivedEventHandler* CertificateReceived
C++/CLI 
[CategoryAttribute("Security")]
[DescriptionAttribute("Raised when a remote certificate has been received for verification.")]
public:
event CertificateReceivedEventHandler^ CertificateReceived

Event Data

The event handler receives an argument of type CertificateReceivedEventArgs containing data related to this event. The following CertificateReceivedEventArgs properties provide information specific to this event.

PropertyDescription
Accept Controls whether or not the certificate is accepted.
Certificate The certificate received for authentication.
RemoteEndPoint Returns the endpoint of the remote host.
TrustedRoot Returns whether or not the certificate is from a Trusted Root Authority.
ValidDate Returns whether or not the current time-date is within the certificate's life span.
ValidName Returns whether or not the name is valid.

Example

The following example demonstrates creating a simple secure client.
Visual BasicCopy Code
Private Sub SecureTest()

    'Allow component to negotiate best security option
    Tcp1.SecureProtocol = SecureProtocol.Auto

    ' Connect to a secure echo server.
    Tcp1.Connect("mysecureserver", 7)

    ' Send secure data
    Tcp1.Send("Test")

    ' Receive and display secure data
    System.Diagnostics.Debug.WriteLine(Tcp1.Receive().ToString())

    ' Close the connection
    Tcp1.Close()
End Sub

Private Sub Tcp1_CertificateRequested(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tcp1.CertificateRequested
    'The server has requested client authentication
    'Allow the user to select a certificate
    Dim certform As New CertificateListForm(True, True)
    If (certform.ShowDialog(Me) = DialogResult.OK) Then
        Tcp1.Certificate = certform.SelectedCertificate
    End If
End Sub

Private Sub Tcp1_CertificateReceived(ByVal sender As Object, ByVal e As Dart.PowerTCP.SslSockets.CertificateReceivedEventArgs) Handles Tcp1.CertificateReceived
    'If the server's certificate is invalid for any reason,
    'the user can decide whether to proceed
    Dim msg As String = "The certificate was invalid for the following reason(s)" + vbLf

    ' Check to see if the certificate is from a trusted root.
    If (Not e.TrustedRoot) Then
        msg += "This certificate is not from a trusted root" + vbLf
    End If

    ' Check to see if the certificate has a valid date.
    If (Not e.ValidDate) Then
        msg += "This certificate does not have a valid date" + vbLf
    End If

    ' Check to see if the certificate has a valid name.
    If (Not e.ValidName) Then
        msg += "This certificate does not have a valid name" + vbLf
    End If

    If (Not e.Accept) Then
        msg += "Would you like to accept this certificate anyway?"
        If (MessageBox.Show(msg, "Invalid Cert Received", MessageBoxButtons.YesNo) = DialogResult.Yes) Then
            e.Accept = True
        End If
    End If
End Sub
C#Copy Code
private void SecureTest()
{
	//Allow component to negotiate best security option
	tcp1.SecureProtocol = SecureProtocol.Auto;

	// Connect to a secure echo server.
	tcp1.Connect("mysecureserver", 7);

	// Send secure data
	tcp1.Send("Test");

	// Receive and display secure data
	System.Diagnostics.Debug.WriteLine(tcp1.Receive().ToString());

	// Close the connection
	tcp1.Close();
}		

private void tcp1_CertificateRequested(object sender, System.EventArgs e)
{
	//The server has requested client authentication
	//Allow the user to select a certificate
	CertificateListForm certform = new CertificateListForm(true, true);
	if(certform.ShowDialog(this) == DialogResult.OK)
		tcp1.Certificate = certform.SelectedCertificate;
}

private void tcp1_CertificateReceived(object sender, CertificateReceivedEventArgs e)
{
	//If the server's certificate is invalid for any reason,
	//the user can decide whether to proceed
	string msg = "The certificate was invalid for the following reason(s)\n";

	// Check to see if the certificate is from a trusted root.
	if(!e.TrustedRoot)
		msg+= "This certificate is not from a trusted root\n";

	// Check to see if the certificate has a valid date.
	if(!e.ValidDate)
		msg+= "This certificate does not have a valid date\n";

	// Check to see if the certificate has a valid name.
	if(!e.ValidName)
		msg+= "This certificate does not have a valid name\n";

	if(!e.Accept)
	{
		msg += "Would you like to accept this certificate anyway?";
		if(MessageBox.Show(msg, "Invalid Cert Received", MessageBoxButtons.YesNo) == DialogResult.Yes)
			e.Accept = true;
	}
}

Remarks

The CertificateReceived event is raised when a certificate has been received from a secure server. A CertificateReceivedEventArgs object is passed into the event handler, containing information required to determine the validity of the certificate. If the certificate is determined to be invalid (if any of the properties CertificateReceivedEventArgs.ValidDate, CertificateReceivedEventArgs.ValidName, or CertificateReceivedEventArgs.TrustedRoot are false) the certificate is rejected. CertificateReceivedEventArgs.Accept is false to signify this. Set CertificateReceivedEventArgs.Accept to true to override this and accept the certificate.

Requirements

Target Platforms: Microsoft .NET Framework 2.0

See Also

Documentation Version 3.2
© 2010 Dart Communications. All Rights Reserved.