Delphi XE2 DataSnap Server - Log client connection user/properties
I'm building an XE2 DataSnap server which will serve connections from REST clients. My DSServerClass LifeCycle property is set to 'Invocation'. What I want to do is to log the details of all client connections to the server, including the following details : username, IP address, protocol, application name. I can currently get these details using the following events :
DSAuthenticationManager - UserAuthenticate() : username, protocol (using the standard parameters passed in)
DSServer - Connect() : protocol, IP address, application name (using DSConnectEventObject.ChannelInfo.ClientInfo)
What I want to do is just log once for all details, but it seems I can't get all the details I need in one event. I tried using a shared private variable in the class but as expected this gave inconsistent results - the wrong IP address against the wrong username. Is there another way to achieve what I want?
You can use TDSServer.OnConnect event (which is called after TDSAuthenticationManager.OnUserAuthenticate). There you have access to ChannelInfo.ClientInfo as you've discovered and also ConnectProperties from which you can read property values like this:
Scheme := ConnectProperties.Values[TDBXPropertyNames.DSAuthenticationScheme]; UserName := ConnectProperties.Values[TDBXPropertyNames.DSAuthenticationUser]; Password := ConnectProperties.Values[TDBXPropertyNames.DSAuthenticationPassword];
procedure TServerContainer1.DSServer1Connect(DSConnectEventObject: TDSConnectEventObject); begin Form1.Memo1.Lines.Add(Format('Conn->UserName=%s, Password=%s', [ DSConnectEventObject.ConnectProperties[TDBXPropertyNames.UserName], DSConnectEventObject.ConnectProperties[TDBXPropertyNames.Password] ])); // 取 Client 端的IP 和 Port Form1.Memo1.Lines.Add('IP =' + DSConnectEventObject.ChannelInfo.ClientInfo.IpAddress + ':' + DSConnectEventObject.ChannelInfo.ClientInfo.ClientPort); end;