[en] Authentication/Encryption (solved)

Topics: Technical Support
Nov 24 at 1:14 PM
Edited Nov 24 at 1:25 PM
I'm looking to have authentication and encrypted communications using TCP between clients and servers that don't have to be Windows-based (could be Linux). The clients can each be connected to one or more servers using multiple Zyan connection and proxy objects. Both clients and servers are Windows services that can be initialized with database settings. There could be usernames and passwords. Some simple encryption is desirable for communications after authentication.

I've read the documentation, and realize that I should implement IAuthenticationProvider in some way. (Is this on the server side?) I'm not quite sure how it is all supposed to work. May I see an example of a likely setup, with client and server code?
Coordinator
Nov 25 at 2:16 AM
Hello Mark,

what you describe is a typical scenario supported by Zyan.
 
I've read the documentation, and realize that I should implement
IAuthenticationProvider in some way. (Is this on the server side?)
 
Yes, the authentication provider is a server-side class that checks the credentials before
letting a client in. Here is a simple authentication provider with the hardcoded login and
password strings (your real application will of course use the database of some sort):
class MyAuthProvider : IAuthenticationProvider
{
    public AuthResponseMessage Authenticate(AuthRequestMessage authRequest)
    {
        // here you check if your login/password is in the database
        if (authRequest.Credentials["Login"].ToString() == "Hacker" &&
            authRequest.Credentials["Password"].ToString() == "Pa55w0rd")
        {
            // ok
            return new AuthResponseMessage
            {
                Success = true,
                AuthenticatedIdentity = new GenericIdentity("Hacker")
            };
        }

        // fail
        return new AuthResponseMessage
        {
            Success = false,
            ErrorMessage = "Access denied"
        };
    }
}
To enable encryption, set the encryption parameter to true in the protocol setup classes:
// server
var proto = new TcpDuplexServerProtocolSetup(port, new MyAuthProvider(), encryption: true);

// client
var proto = new TcpDuplexClientProtocolSetup(encryption: true);
I've just uploaded the complete sample code to this Gist:
https://gist.github.com/yallie/1e31bef306f7c53db8b9e9e23187c10f

It's a tiny console program that acts both as a client and a server.
Compile the program and start it up 3 times.
You'll have three local peers ready to connect to each other.

Type in a few words in the console and press ENTER to send a message to all
connected peers. Press ENTER to shut down the peer and close its connections.

Hope that helps.
Regards, Alex.
Marked as answer by yallie on 11/28/2016 at 11:41 AM