带有令牌的.NET FHIR客户端示例

时间:2018-10-30 11:09:00

标签: .net hl7-fhir

正在尝试寻找利用fhir-net-api创建FHIR客户端服务器端并将授权令牌从智能型客户端应用程序传递回我的Web服务器以进行制作的示例。在.net fhir文档中对FHIR服务器进行调用之前,调用已生成/有效令牌的FHIR服务器,并且未找到将令牌添加到FHIR客户端的任何示例,因为这些示例均已进入公共端点。

我只是将其添加为搜索参数,还是在调用需要令牌的非公共API时需要做些事情来利用令牌吗?我注意到搜索参数中存在令牌类型,但不确定如何利用它。...这是一个对观察端点进行通用搜索调用的基本示例,我认为我需要将令牌添加为搜索参数:

_fhirClient = new FhirClient(openApi);
_fhirClient.PreferredFormat = ResourceFormat.Json;
_fhirSearchParamaters = new SearchParams();
_fhirSearchParamaters.Add("patient", mrn);
//Not sure where to add this token to the FHIR client 
//before executing the search call to get the bundle from the FHIR server...
_fhirSearchParamaters.Add("token", token);
_fhirSearchParamaters.Add("code", "58941-6");
//return the bundle from the FHIR server
return _fhirClient.Search(_fhirSearchParamaters);

2 个答案:

答案 0 :(得分:2)

您可以像这样在客户端的OnBeforeRequest事件中向调用添加标头:

_fhirClient.OnBeforeRequest += (object sender, BeforeRequestEventArgs e) =>
{
        // Replace with a valid bearer token for the server
        e.RawRequest.Headers.Add("Authorization", "Bearer XXXXXXX");
};

此文档可在以下位置找到:http://docs.simplifier.net/fhirnetapi/client/request-response.html#fhirclient-event-handlers

答案 1 :(得分:0)

看起来像使用2.0版本的新方法:

        var messageHandler = new HttpClientEventHandler();

        string token = {{get your token}};
        messageHandler.OnBeforeRequest += (object sender, BeforeHttpRequestEventArgs e) =>
        {
            e.RawRequest.Headers
            .Add("Authorization", $"Bearer {token}");

            ////var request = Encoding.UTF8.GetString(e.Body, 0, e.Body.Length);
        };

        Hl7.Fhir.Rest.FhirClient client = new Hl7.Fhir.Rest.FhirClient(endpoint, messageHandler: messageHandler, settings: new FhirClientSettings()
        {
            PreferredFormat = ResourceFormat.Json
        });