You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 7中如何通过HTTPS与Cookie认证连接WCF服务

问题分析与解决思路

从你提供的请求对比来看,问题并非HTTPS本身,而是请求格式差异和Cookie传递错误导致的403禁止访问,具体差异和修复方案如下:

核心问题点

  1. Cookie格式错误

    • .NET Framework 4.8的Cookie使用分号+空格分隔多个Cookie(符合HTTP标准),但你的.NET 7请求里用了逗号分隔,服务器无法正确解析。
    • 代码中错误地给Cookie添加了Basic 前缀(这是HTTP Basic认证的格式,你的场景是Cookie认证,完全不需要)。
    • 重复添加Cookie键会导致覆盖,最终只有最后一个Cookie被传递。
  2. WS-Addressing头不兼容
    .NET 7的WCF客户端默认添加了WS-Addressing(http://www.w3.org/2005/08/addressing)的SOAP头,而.NET Framework的请求中没有该头,服务器可能不支持这种格式。

  3. Content-Type头缺失参数
    .NET 4.8的请求里Content-Type包含action="",但.NET 7的请求中缺少该参数,可能触发服务器的校验逻辑。

  4. WCF安全配置冗余
    服务器采用Cookie+XSRF自定义认证,无需WCF Transport层的客户端凭证认证,错误的ClientCredentialType设置会导致服务器返回403。


具体修复步骤

1. 修正Cookie传递格式

HTTP标准中多个Cookie必须用; (分号+空格)分隔,且不需要Basic 前缀,合并所有Cookie为一个字符串传递:

方式一:OperationContextScope

using (new OperationContextScope(port.InnerChannel)) {
    var httpRequest = new HttpRequestMessageProperty();
    // 添加XSRF头
    httpRequest.Headers.Add("X-XSRF-TOKEN", xsrfToken?.Value);
    // 合并Cookie为一个字符串,用分号分隔
    httpRequest.Headers.Add(HttpRequestHeader.Cookie, $"JSESSIONID={sessionId.Value}; XSRF-TOKEN={xsrfToken.Value}");
    // 修复Content-Type,添加action参数
    httpRequest.ContentType = "application/soap+xml; charset=utf-8; action=\"\"";
    
    OperationContext.Current.OutgoingMessageProperties.Add(HttpRequestMessageProperty.Name, httpRequest);

    await IspisVerzije(port);
    port.Close();
}

方式二:HttpHeaderMessageInspector

static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials) {
    var headers = new Dictionary<string, string> {
        ["X-XSRF-TOKEN"] = Program.xsrfToken?.Value,
        // 合并Cookie,去掉错误的Basic前缀
        ["Cookie"] = $"JSESSIONID={Program.sessionId.Value}; XSRF-TOKEN={Program.xsrfToken.Value}",
        ["Content-Type"] = "application/soap+xml; charset=utf-8; action=\"\""
    };

    var behaviour = new AddHttpHeaderMessageEndpointBehavior(headers);
    serviceEndpoint.EndpointBehaviors.Add(behaviour);
}

2. 移除WS-Addressing头

修改WCF绑定的MessageVersion,使其与.NET Framework一致(仅使用Soap12,不带WS-Addressing):

// 创建自定义绑定,移除WS-Addressing
var binding = new CustomBinding();
// 添加文本消息编码,设置为Soap12(无WS-Addressing)
binding.Elements.Add(new TextMessageEncodingBindingElement(MessageVersion.Soap12, Encoding.UTF8));
// 添加HTTPS传输
binding.Elements.Add(new HttpsTransportBindingElement());

// 初始化客户端时使用该绑定
var client = new IntegrationWSv2Client(binding, new EndpointAddress(Config.URL_APP_ORIGIN));

3. 调整WCF安全配置

设置Transport安全模式,但禁用客户端凭证认证:

var binding = new BasicHttpsBinding(BasicHttpsSecurityMode.Transport);
// 不需要WCF自动添加客户端认证
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;

4. 验证请求一致性

修复后用Fiddler对比请求,确保与.NET Framework 4.8的请求一致:

  • Cookie头格式正确(分号分隔)
  • X-XSRF-TOKEN头值匹配
  • Content-Type包含action=""
  • SOAP信封无WS-Addressing头
  • SOAP Body结构完全一致

内容的提问来源于stack exchange,提问作者mariob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 00:50:29