批量请求承载令牌

时间:2017-10-16 10:29:55

标签: c# .net bearer-token

在我的.net框架Web应用程序中设置对批处理请求的支持。即使我将持有者令牌复制到每个单独的请求中,每个单独的请求也会被禁止返回。 我的批处理支持的集成测试是下面的 - 我的ServerHelper代码为请求添加了承载令牌。

[TestMethod]
public async Task BatchRequestTest()
{
    var requestMessage = new HttpRequestMessage(HttpMethod.Get, ServerHelper.HttpClient.BaseAddress + "values/22");

    requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", await ServerHelper.GetAccessToken());

    var getMetricsRequest = new HttpMessageContent(requestMessage);
    var getTransactionsRequest1 = new HttpMessageContent(new HttpRequestMessage(HttpMethod.Get, ServerHelper.HttpClient.BaseAddress + "values/23"));
    var failingRequest = new HttpMessageContent(new HttpRequestMessage(HttpMethod.Get, ServerHelper.HttpClient.BaseAddress + "values/77"));
    MultipartContent content = new MultipartContent("mixed", "batch_" + Guid.NewGuid().ToString());

    content.Add(getMetricsRequest);
    content.Add(getTransactionsRequest1);
    content.Add(failingRequest);


    HttpRequestMessage batchRequest = new HttpRequestMessage(HttpMethod.Post, ServerHelper.HttpClient.BaseAddress + "batch")
    {
        Content = content
    };

    var response = await ServerHelper.HttpClient.SendAsync(batchRequest);
    Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);//assert true


    var responseContents = await response.Content.ReadAsMultipartAsync();

    var get1 = await responseContents.Contents[0].ReadAsHttpResponseMessageAsync();
    var get2 = await responseContents.Contents[1].ReadAsHttpResponseMessageAsync();
    var get3 = await responseContents.Contents[2].ReadAsHttpResponseMessageAsync();


    Assert.AreEqual(HttpStatusCode.OK, get1.StatusCode);//assert fail
    Assert.AreEqual(HttpStatusCode.OK, get2.StatusCode);//assert fail
    Assert.AreEqual(HttpStatusCode.NotFound, get3.StatusCode);
}

我的部分配置负责添加批量支持:

var server = new HttpServer(httpConfiguration);

appBuilder.UseWebApi(server);

httpConfiguration.Routes.MapHttpBatchRoute(
    routeName: "batch",
    routeTemplate: "batch",
    batchHandler: new DefaultHttpBatchHandler(server)
    {
        ExecutionOrder = BatchExecutionOrder.NonSequential
    }
);

我是否需要在管道中添加内容?

2 个答案:

答案 0 :(得分:0)

请注意,您已将Authorization标头添加到另一个名为requestMessage的局部变量中。尝试将授权标头添加到batchRequest变量和/或使用HttpMessageContent创建其他requestMessage实例作为构造函数参数。

答案 1 :(得分:0)

今天遇到了这个问题,所以我想我会发布一个解决方案,以防其他人对此感到奇怪。您可以通过扩展DefaultHttpBatchHandler来创建自定义批处理程序。然后,您可以在子请求中设置授权标头。

public class AuthorizationBatchHandler : DefaultHttpBatchHandler
{
    public AuthorizationBatchHandler(HttpServer httpServer) : base(httpServer)
    {
    }

    public override async Task<IList<HttpRequestMessage>> ParseBatchRequestsAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var requests = await base.ParseBatchRequestsAsync(request, cancellationToken);

        foreach (var childRequest in requests)
        {
            childRequest.Headers.Authorization = request.Headers.Authorization;
        }
        return requests;
    }
}

然后在启动方法中将其连接起来,如下所示:

        httpConfiguration.Routes.MapHttpBatchRoute(
            routeName: "batch",
            routeTemplate: "batch",
            batchHandler: new AuthorizationBatchHandler(server));

那就是要做的一切。