Dart.PowerTCP.SslSockets Namespace > Server Class : CertificateReceived Event (Server) |
This event is raised when a certificate has been received to be authenticated.
[Visual Basic]
<DescriptionAttribute("Raised when a certificate is received for authentication")>
Public Event CertificateReceived() As CertificateReceivedEventHandler
[C#]
[DescriptionAttribute("Raised when a certificate is received for authentication")]
public event CertificateReceivedEventHandler CertificateReceived();
[C++]
[DescriptionAttribute("Raised when a certificate is received for authentication")]
public: __event CertificateReceivedEventHandler* CertificateReceived();
[C++/CLI]
[DescriptionAttribute("Raised when a certificate is received for authentication")]
public:
event CertificateReceivedEventHandler^ CertificateReceived();
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.
Property | Description |
---|---|
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. |
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 ValidDate, ValidName, or TrustedRoot are false) the certificate is rejected. Accept is false to signify this. Set Accept to true to override this and accept the certificate.
The following example demonstrates creating a simple secure server that requires client authentication.
[Visual Basic]
Private Sub StartSecureServer()
' Require ClientAuthentication
Server1.AuthenticateClient = True
' Create a new CertificateStore object for "MY" certificate store.
Dim store As New CertificateStore(CertificateStoreLocation.LocalMachine, CertificateStoreName.My)
' Set the certificate to the default certificate.
Server1.Certificate = store(0)
' Begin listening for connections.
Server1.Listen(443)
End Sub
Private Sub Server1_Connection(ByVal sender As Object, ByVal e As ConnectionEventArgs) Handles Server1.Connection
Try
' Keep reading and echoing until the client closes the connection.
Dim s As String = e.Tcp.Receive().ToString()
While e.Tcp.Connected
e.Tcp.Send(s)
s = e.Tcp.Receive().ToString()
End While
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
End Sub
Private Sub Server1_CertificateReceived(ByVal sender As Object, ByRef e As CertificateReceivedEventArgs) Handles Server1.CertificateReceived
Dim msg As String = "The certificate was invalid for the following reason(s)" + vbCrLf
' 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" + vbCrLf
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" + vbCrLf
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" + vbCrLf
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#]
private void StartSecureServer()
{
// Require ClientAuthentication
server1.AuthenticateClient = true;
// Create a new CertificateStore object for "MY" certificate store.
CertificateStore store = new CertificateStore(CertificateStoreLocation.LocalMachine, CertificateStoreName.My);
// Set the certificate to the default certificate.
server1.Certificate = store[0];
// Begin listening for connections.
server1.Listen(443);
}
private void server1_Connection(object
sender, ConnectionEventArgs e)
{
try
{
// Keep reading and echoing until the client closes the connection.
string s = e.Tcp.Receive().ToString();
while (e.Tcp.Connected)
{
e.Tcp.Send(s);
s = e.Tcp.Receive().ToString();
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
private void server1_CertificateReceived(object sender, CertificateReceivedEventArgs e)
{
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;
}
}
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
Send comments on this topic.
Documentation version 1.1.2.0.
© 2008 Dart Communications. All rights reserved.