// Callback which is called when a client sends the login message. // It shall verify the login and return the handshake message. private static object GetHandshakeMessage(string channelId, string responseReceiverId, object loginMessage) { // Find the login name and password in "database" // and encrypt the handshake message. if (loginMessage is string) { string aLoginName = (string)loginMessage; Console.WriteLine("Received login: "******"Login was not ok. The connection will be closed."); return(null); } } } else { return(null); } }
// Callback which is called when a client sends the handshake response message. private static bool Authenticate(string channelId, string responseReceiverId, object loginMessage, object handshakeMessage, object handshakeResponseMessage) { string aPassword; if (loginMessage is string) { using (LogstorOEEEntities db = new TestLogin.LogstorOEEEntities()) { string aLoginName = (string)loginMessage; Users_Security users_Security = db.Users_Security.Where((x) => x.Login == aLoginName).FirstOrDefault(); if (!String.IsNullOrEmpty(users_Security.Password)) { aPassword = users_Security.Password; } else { return(false); } } // Get the password associated with the user. // Decrypt the handshake response message. // Handshake response message is one more time encrypted handshake message. // Therefore if the handshake response is decrypted two times it should be // the originaly generated GUID. try { ISerializer aSerializer = new AesSerializer(aPassword); // Decrypt handshake response to get original GUID. string aDecodedHandshakeResponse1 = aSerializer.Deserialize <string>(handshakeResponseMessage); byte[] temp = ConvertHandshakeToBytes(aDecodedHandshakeResponse1); string aDecodedHandshakeResponse2 = aSerializer.Deserialize <string>(temp); // Decrypt original handshake message. string anOriginalGuid = aSerializer.Deserialize <string>(handshakeMessage); // If GUIDs are equal then the identity of the client is verified. if (anOriginalGuid == aDecodedHandshakeResponse2) { Console.WriteLine("Client authenticated."); // The handshake response is correct so the connection can be established. return(true); } } catch (Exception err) { // Decoding of the response message failed. // The authentication will not pass. Console.WriteLine("Decoding handshake message failed.", err); } } // Authentication did not pass. Console.WriteLine("Authentication did not pass. The connection will be closed."); return(false); }