PowerTCP Sockets for .NET
AuthenticateAsServer Method (TcpBase)
Example 




Security parameters used during authentication.
Authenticates a client when establishing a secure connection.
Syntax
'Declaration
 
<SecuritySafeCriticalAttribute()>
Public Sub AuthenticateAsServer( _
   ByVal security As ServerSecurity _
) 
'Usage
 
Dim instance As TcpBase
Dim security As ServerSecurity
 
instance.AuthenticateAsServer(security)
[SecuritySafeCritical()]
public void AuthenticateAsServer( 
   ServerSecurity security
)
[SecuritySafeCritical()]
public: void AuthenticateAsServer( 
   ServerSecurity* security
) 
[SecuritySafeCritical()]
public:
void AuthenticateAsServer( 
   ServerSecurity^ security
) 

Parameters

security
Security parameters used during authentication.
Remarks
To accept or reject a certificate "on-the-fly" implement a Security.ValidationCallback function.
Example
This example demonstrates AuthenticateAsServer().
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;

/// <summary>
/// Object containing server security configuration
/// </summary>
ServerSecurity security = new ServerSecurity();

/// <summary>
/// Starts the server listening on port 7. server1_NewConnection will 
/// be invoked on an IO completion thread as each clients connects.
/// </summary>
private void button1_Click(object sender, EventArgs e)
{
    //Select certificate and set client certificate validation callback
    ConfigSecurity();

    //Start the echo server on port 7
    server1.Start(server1_NewConnection, 7, null);
}

/// <summary>
/// Finds and loads the certificate used for authenticating the server, and sets the ValidationCallback.
/// </summary>
/// <remarks>
/// This snippet assumes the presence of the Dart Test Certificate in the user's 'My' certificate store. 
/// The Dart Test Certificate may be installed from the component's Start Menu program group.
/// </remarks>
private void ConfigSecurity()
{
    //Try to find the Dart Test Certificate
    X509Store certificateStore = new X509Store(StoreName.My);
    certificateStore.Open(OpenFlags.ReadOnly);
    foreach (X509Certificate2 cert in certificateStore.Certificates)
        if (cert.GetNameInfo(X509NameType.SimpleName, true) == "DartdomCA")
        {
            security.Certificate = cert;
            break;
        }
    certificateStore.Close();

    security.ValidationCallback = ClientCertificateValidation;
}

/// <summary>
/// Invoked on an IO completion thread as each client connects.
/// </summary>
/// <param name="client">The connection to the client</param>
/// <param name="state">Used to pass in data from the 'state' parameter of Server.Start(). Not used in this snippet.</param>
private void server1_NewConnection(Tcp client, object state)
{
    //Establish SSL immediately after client connects. This is commonly known as implicit SSL.
    try
    {
        client.AuthenticateAsServer(security);
    }
    catch (Exception ex)
    {
        //If an error is encountered, disconnect the client. Otherwise the connection will remain open.
        client.Close();
        //Rethrow exception to be automatically marshaled to server1_Error event (not included in this snippet)
        throw ex;
    }

    //Implement protocol logic here, such as an echo server:
    byte[] buffer = new byte[1024];
    Data data = client.Read(buffer);
    while (data != null)
    {
        client.Write(data.Buffer, data.Offset, data.Count);
        data = client.Read(buffer);
    }
}

/// <summary>
/// Presents the client's certificate for validation.
/// </summary>
/// <param name="certificate">The certificate presented by the client.</param>
/// <param name="chain">The chain of certificate authorities associated with the remote certificate.</param>
/// <param name="sslPolicyErrors">One or more errors associated with the client's certificate.</param>
/// <returns>A Boolean value that specifies whether the provided certificate is accepted for authentication.</returns>
private bool ClientCertificateValidation(Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    //Accept all clients in this snippet. See the AuthenticateAsClient() snippet for a demonstration of examining sslPolicyErrors.
    return true;
}
Imports System.Net.Security
Imports System.Security.Authentication
Imports System.Security.Cryptography.X509Certificates

''' <summary>
''' Object containing server security configuration
''' </summary>
Private security As New ServerSecurity()

''' <summary>
''' Starts the server listening on port 7. server1_NewConnection will 
''' be invoked on an IO completion thread as each clients connects.
''' </summary>
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    'Select certificate and set client certificate validation callback
    ConfigSecurity()

    'Start the echo server on port 7
    server1.Start(AddressOf server1_NewConnection, 7, Nothing)
End Sub

''' <summary>
''' Finds and loads the certificate used for authenticating the server, and sets the ValidationCallback.
''' </summary>
''' <remarks>
''' This snippet assumes the presence of the Dart Test Certificate in the user's 'My' certificate store. 
''' The Dart Test Certificate may be installed from the component's Start Menu program group.
''' </remarks>
Private Sub ConfigSecurity()
    'Try to find the Dart Test Certificate
    Dim certificateStore As New X509Store(StoreName.My)
    certificateStore.Open(OpenFlags.ReadOnly)
    For Each cert As X509Certificate2 In certificateStore.Certificates
        If cert.GetNameInfo(X509NameType.SimpleName, True) = "DartdomCA" Then
            security.Certificate = cert
            Exit For
        End If
    Next cert
    certificateStore.Close()

    security.ValidationCallback = AddressOf ClientCertificateValidation
End Sub

''' <summary>
''' Invoked on an IO completion thread as each client connects.
''' </summary>
''' <param name="client">The connection to the client</param>
''' <param name="state">Used to pass in data from the 'state' parameter of Server.Start(). Not used in this snippet.</param>
Private Sub server1_NewConnection(ByVal client As Tcp, ByVal state As Object)
    'Establish SSL immediately after client connects. This is commonly known as implicit SSL.
    Try
        client.AuthenticateAsServer(security)
    Catch ex As Exception
        'If an error is encountered, disconnect the client. Otherwise the connection will remain open.
        client.Close()
        'Rethrow exception to be automatically marshaled to server1_Error event (not included in this snippet)
        Throw ex
    End Try

    'Implement protocol logic here, such as an echo server:
    Dim buffer(1023) As Byte
    Dim data As Data = client.Read(buffer)
    Do While data IsNot Nothing
        client.Write(data.Buffer, data.Offset, data.Count)
        data = client.Read(buffer)
    Loop
End Sub

''' <summary>
''' Presents the client's certificate for validation.
''' </summary>
''' <param name="certificate">The certificate presented by the client.</param>
''' <param name="chain">The chain of certificate authorities associated with the remote certificate.</param>
''' <param name="sslPolicyErrors">One or more errors associated with the client's certificate.</param>
''' <returns>A Boolean value that specifies whether the provided certificate is accepted for authentication.</returns>
Private Function ClientCertificateValidation(ByVal sender As Object, ByVal certificate As X509Certificate, ByVal chain As X509Chain, ByVal sslPolicyErrors As SslPolicyErrors) As Boolean
    'Accept all clients in this snippet. See the AuthenticateAsClient() snippet for a demonstration of examining sslPolicyErrors.
    Return True
End Function
See Also

Reference

TcpBase Class
TcpBase Members


PowerTCP Sockets for .NET Documentation Version 4.5
© 2018 Dart Communications. All Rights Reserved.
Send comments on this topic