diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/AbpWebhooksOptions.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/AbpWebhooksOptions.cs index 1411be14f..137834908 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/AbpWebhooksOptions.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks.Core/LINGYUN/Abp/Webhooks/AbpWebhooksOptions.cs @@ -28,6 +28,10 @@ public class AbpWebhooksOptions public HashSet DeletedWebhooks { get; } public HashSet DeletedWebhookGroups { get; } + /// + /// 默认请求头 + /// + public IDictionary DefaultHttpHeaders { get; } public AbpWebhooksOptions() { @@ -39,5 +43,24 @@ public class AbpWebhooksOptions DeletedWebhooks = new HashSet(); DeletedWebhookGroups = new HashSet(); + + DefaultHttpHeaders = new Dictionary + { + // 取消响应内容包装 + { "_AbpDontWrapResult", "true" }, + // TODO: 可能跨域影响 + // { "X-Requested-With", "XMLHttpRequest" }, + // 标识来源 + { "X-Requested-From", "abp-webhooks" }, + }; + } + + public void AddHeader(string key, string value) + { + if (value.IsNullOrWhiteSpace()) + { + return; + } + DefaultHttpHeaders[key] = value; } } diff --git a/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks/LINGYUN/Abp/Webhooks/DefaultWebhookSender.cs b/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks/LINGYUN/Abp/Webhooks/DefaultWebhookSender.cs index 5bf66f408..9cfb6a030 100644 --- a/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks/LINGYUN/Abp/Webhooks/DefaultWebhookSender.cs +++ b/aspnet-core/modules/webhooks/LINGYUN.Abp.Webhooks/LINGYUN/Abp/Webhooks/DefaultWebhookSender.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Options; using System; using System.Collections.Generic; using System.Linq; @@ -15,6 +16,7 @@ namespace LINGYUN.Abp.Webhooks { public ILogger Logger { protected get; set; } + private readonly AbpWebhooksOptions _options; private readonly IWebhookManager _webhookManager; private readonly IHttpClientFactory _httpClientFactory; @@ -22,8 +24,10 @@ namespace LINGYUN.Abp.Webhooks public DefaultWebhookSender( IWebhookManager webhookManager, - IHttpClientFactory httpClientFactory) + IHttpClientFactory httpClientFactory, + IOptions options) { + _options = options.Value; _webhookManager = webhookManager; _httpClientFactory = httpClientFactory; @@ -110,6 +114,19 @@ namespace LINGYUN.Abp.Webhooks protected virtual void AddAdditionalHeaders(HttpRequestMessage request, WebhookSenderArgs webhookSenderArgs) { + foreach (var header in _options.DefaultHttpHeaders) + { + if (request.Headers.TryAddWithoutValidation(header.Key, header.Value)) + { + continue; + } + + if (request.Content.Headers.TryAddWithoutValidation(header.Key, header.Value)) + { + continue; + } + } + foreach (var header in webhookSenderArgs.Headers) { if (request.Headers.TryAddWithoutValidation(header.Key, header.Value)) @@ -144,7 +161,7 @@ namespace LINGYUN.Abp.Webhooks res.Add(header.Key, header.Value.JoinAsString(";")); } } - + return res; } }