c# - 通过生成的 WebServiceClient 进行 WCF Soap 基本身份验证

标签 c# web-services wcf soap soap-client

我正在尝试使用 WCF Web 服务引用来使用 SOAP Web 服务。

我已经能够使用 System.Web.Servicees Web 服务引用在 .NET 4.8 框架项目中成功使用 SOAP Web 服务。但是我需要在 .NET Core 项目中使用 Web 服务。从 WSDL 生成的 WCF 类与 .NET Framework Web 服务不同。看来您现在必须使用生成的 WebServiceClient 来与 Web 服务交互。

我相信 Web 服务需要基本身份验证,因为我能够在 .NET Framework 项目中使用基本身份验证进行身份验证。

这是当我尝试执行 Web 服务的方法之一时收到的错误消息。

  System.ServiceModel.Security.MessageSecurityException
  HResult=0x80131500
  Message=The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was ''.
  Source=System.Private.ServiceModel

这是我的代码,它实例化客户端并调用方法

    var callContext = new CAdxCallContext();
    callContext.codeLang = "ENG";
    callContext.poolAlias = "BGRTEST";
    callContext.requestConfig = "adxwss.trace.on=on&adxwss.trace.size=16384&adonix.trace.on=on&adonix.trace.level=3&adonix.trace.size=8";


    var proxy = new CAdxWebServiceXmlCCClient();
    proxy.ClientCredentials.UserName.UserName = "username";
    proxy.ClientCredentials.UserName.Password = "password";

    string _InputXml = "<PARAM>" +
    "<GRP ID= \"GRP1\">" +
    "<FLD NAME = \"ITMREF\">" + 100001 + "</FLD>" +
    "</GRP>" +
    "</PARAM>";

    try
    {
        var response = proxy.run(callContext, "BGR_SIEPRO", _InputXml);
    }
    finally
    {
        proxy.Close();
    }

我的WCF服务连接: WCF Connected Service Screenshot

自动生成的 WCF WebServiceClient:https://github.com/abiddle-bgr/Test/blob/main/CAdxWebServiceXmlCCClient.cs

最佳答案

我最终不得不通过创建自定义端点并将其作为自定义端点行为添加到代理来手动注入(inject) header 。

 var proxy = new CAdxWebServiceXmlCCClient();
 proxy.Endpoint.EndpointBehaviors.Add(new CustomEndpoint());

 public class ClientMessageInspector : IClientMessageInspector
    {
        public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            // Nothing Here
            Console.Write(reply.ToString());
        }

        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
            httpRequestProperty.Headers[HttpRequestHeader.Authorization] = "Basic " +
                Convert.ToBase64String(Encoding.ASCII.GetBytes("USERNAME" + ":" +
                    "PASSWORD"));
            request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestProperty);
            return null;
        }
    }

    public class CustomEndpoint : IEndpointBehavior
    {
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
            // Nothing here
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            clientRuntime.ClientMessageInspectors.Add(new ClientMessageInspector());
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            // Nothing here
        }

        public void Validate(ServiceEndpoint endpoint)
        {
            // Nothing here
        }
    }

关于c# - 通过生成的 WebServiceClient 进行 WCF Soap 基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71413195/

相关文章:

c# - 如果 catch 和 finally block 都抛出异常会发生什么?

java - 优化 Web 服务的 XML 响应

visual-studio - 通过 WSDL 添加对 Visual Studio 的服务引用时出错

.net - 使用 .NET/WIF/WCF 查询 WS-Trust 1.4 STS

c# - 如何从用户控件调用一个方法到aspx页面?

c# - 如何编写通用 IEnumerable<SelectListItem> 扩展方法

c# - linq groupby Months 并将任何缺失的月份添加到分组数据中

javascript - 任何对免费互联网时间网络服务的引用

c# - log4net 是否会杀死我的 WCF 单元测试?

.net - 我应该在 IIS 中托管我的 WCF 服务吗?