TsgcWebSocketServer事件 › OnAuthentication

OnAuthentication 事件

当启用身份验证时触发,以便应用程序检查用户名和密码并接受或拒绝连接。

语法

property OnAuthentication: TsgcWSAuthenticationEvent;
// TsgcWSAuthenticationEvent = procedure(Connection: TsgcWSConnection; aUser, aPassword: String; var Authenticated: Boolean) of object

默认值

备注

每当服务器收到来自客户端的身份验证请求时,只要 Authentication.Enabled 为 True 且身份验证方法为内置方案之一(Basic、Session、URL),就会触发 OnAuthentication。aUser 和 aPassword 参数携带客户端提供的凭据;将 Authenticated 设为 True 以接受连接,设为 False 以拒绝连接。若凭据在此未经验证,将使用内置的 AuthUsers 列表作为回退。对于组件无法识别的自定义方案(例如 JWT Bearer 令牌),请改用 OnUnknownAuthentication。

示例


procedure OnAuthentication(Connection: TsgcWSConnection; aUser, aPassword: string;
  var Authenticated: Boolean);
begin
  if ((aUser = 'user') and (aPassword = 'secret')) then
    Authenticated := True
  else
    Authenticated := False;
end;

返回事件