【问题标题】:How to get a token passed as header using datasnap?如何使用 datasnap 获取作为标头传递的令牌?
【发布时间】:2023-12-11 09:17:01
【问题描述】:

在我的客户端应用程序中,我使用以下代码在标头中添加令牌:

RESTRequest.Params.AddItem('Authorization', 'Bearer ' + MyToken, TRESTRequestParameterKind.pkHTTPHEADER, [TRESTRequestParameterOption.poDoNotEncode]);

我想使用 datasnap 在我的服务器中获取此令牌。

我尝试使用herehere 的答案,但没有成功。

有可能吗?

我该怎么做?

编辑

我可以验证Datasnap 执行TIdCustomHTTPServer.DoParseAuthentication 并且DoParseAuthentication 调用FOnParseAuthentication(如果已分配)。

那么,我如何破解 Datasnap 来分配我自己的 OnParseAuthentication

我认为这解决了我的问题。

【问题讨论】:

  • 你确定信息到达目的地,即你能检查进来的HTTP吗?
  • 是的,我确定@JanDoggen。我可以看到这个信息调试单元 IdCustomHTTPServer 的 PreparePostStream 方法。它调用函数TIdCustomHTTPServer.DoParseAuthentication。它使用默认解析器进行身份验证。也许我可以得到Datasnap内部使用的IdHttpServer的OnParserAuthentication,但我不知道该怎么做。
  • 对不起,我说的是PreparePostStream,但它是TIdCustomHTTPServer.DoExecute
  • @DanielGrillo,我也有同样的问题,你解决了吗?
  • @JeffersonRudolf Faça a pergunta no SO em português que eu respondo lá.

标签: delphi http-headers firemonkey indy datasnap


【解决方案1】:

我也有同样的问题。如果使用了 Authentication 标头,那么我们会收到 EIdHTTPUnsupportedAuthorisationScheme 错误,我需要设置 OnParseAuthentication。我今天才开始研究这个,在测试应用“桌面”中我可以执行以下操作。

procedure TMainForm.FormCreate(Sender: TObject);
begin
  FServer := TIdHTTPWebBrokerBridge.Create(Self);
  FServer.OnParseAuthentication := DoParseAuthentication;// <-added this line
end;

现在我需要弄清楚如何更新 ASAPI dll 来做同样的事情。

【讨论】:

  • DoParseAuthentication 在哪里?它的功能受到保护