Dart.PowerTCP.SslSockets Namespace > Server Class : Certificate Property (Server) |
Gets or sets the Certificate object to use if the server is secure.
[Visual Basic]
<DescriptionAttribute("The Certificate used by this Server")>
Public Property Certificate As Certificate
[C#]
[DescriptionAttribute("The Certificate used by this Server")]
public Certificate Certificate {get; set;}
[C++]
[DescriptionAttribute("The Certificate used by this Server")]
public: __property Certificate* get_Certificate();
public: __property void set_Certificate(
Certificate* value
);
[C++/CLI]
[DescriptionAttribute("The Certificate used by this Server")]
public:
property Certificate^ Certificate {
Certificate^ get();
void set (Certificate^ value);
}
The Certificate object representing the certificate to use.
Only use this property if you wish to create a secure server.
When this property is set to a valid certificate, all TCP connections to the server is sent the certificate for server authentication. If successful, all communication between server and client will occur using SSL.
To change back to non-secure operation, set Server.Certificate to null.
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.