如何使用Datasnap将令牌作为标头传递?

8
在我的客户端应用程序中,我使用以下代码将令牌添加到报头:
RESTRequest.Params.AddItem('Authorization', 'Bearer ' + MyToken, TRESTRequestParameterKind.pkHTTPHEADER, [TRESTRequestParameterOption.poDoNotEncode]);

我想通过Datasnap在我的服务器中获取这个令牌。
我尝试使用这里这里的答案,但没有成功。
这可能吗?
我该怎么做?
编辑
我可以验证Datasnap执行TIdCustomHTTPServer.DoParseAuthentication,并且如果它被分配,DoParseAuthentication将调用FOnParseAuthentication。
那么,我如何入侵Datasnap以分配我的自定义OnParseAuthentication?
我认为这解决了我的问题。

你确定信息已经到达目的地了吗?也就是说,你能检查到传入的HTTP请求吗? - Jan Doggen
是的,我确定 @JanDoggen。我可以通过调试 unit IdCustomHTTPServer 的 PreparePostStream 方法来查看这些信息。它调用了函数 TIdCustomHTTPServer.DoParseAuthentication。并且它使用默认的身份验证解析器。也许我可以获取 Datasnap 内部使用的 IdHttpServer 的 OnParserAuthentication,但我不知道如何做到这一点。 - Daniel Grillo
抱歉,我说错了,应该使用TIdCustomHTTPServer.DoExecute而不是PreparePostStream - Daniel Grillo
1
@JeffersonRudolf 在 Stack Overflow 上用葡萄牙语提出问题,我会在那里回答。 - Daniel Grillo
@DanielGrillom 没问题 - Jefferson Rudolf
显示剩余3条评论
2个回答

4

我有同样的问题。如果使用身份验证头,则会出现EIdHTTPUnsupportedAuthorisationScheme错误,我需要设置OnParseAuthentication。我今天刚开始研究这个问题,在“桌面”测试应用程序中,我可以执行以下操作。

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

现在我需要找出如何更新ASAPI dll以实现相同的功能。


DoParseAuthentication在哪里?它的函数是受保护的。 - Jefferson Rudolf

0
procedure TForm1.DoParseAuthentication(AContext: TIdContext;
  const AAuthType, AAuthData: String; var VUsername, VPassword: String;
  var VHandled: Boolean);
var
  AuthValue: String;
begin
  if SameText(AAuthType, 'Bearer') then
  begin
    // AAuthData should contain the bearer token
    // You should validate the token here and set the VUsername or VPassword
    // to the corresponding values if the token is valid.
    AuthValue := AAuthData;
    
    // Add your token validation logic here. If the token is valid, you could 
    // set VUsername to the corresponding username and VHandled to True.
    
    // Example:
    // if ValidateToken(AuthValue) then
    // begin
    //   VUsername := GetUserNameFromToken(AuthValue);
    //   VHandled := True;
    // end;

    VHandled := True;
  end;
end;

你的回答可以通过提供更多支持性信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人能够确认你的回答是否正确。你可以在帮助中心找到关于如何撰写好回答的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接