Web Services 通过生成的WebServiceClient进行WCF Soap基本身份验证

sigwle7e  于 2022-11-15  发布在  其他
关注(0)|答案(2)|浏览(159)

我正在尝试使用WCF Web服务引用使用SOAP Web服务。
我已经能够使用System.Web.Servicees Web服务引用成功使用.NET 4.8 Framework项目中的SOAP Web服务。但是,我需要使用.NET核心项目中的Web服务。从WSDL生成的WCF类与.NET Framework Web服务不同。现在似乎必须使用生成的WebServiceClient才能与Web服务交互。
我认为Web服务需要基本身份验证,因为我能够在.NET框架项目中使用基本身份验证进行身份验证。
以下是我尝试执行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

sc4hvdpw

sc4hvdpw1#

是否设置了安全传输模式?类似于:WCF客户端中基本身份验证

<security mode="TransportCredentialOnly">
      <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" />
      <message clientCredentialType="UserName" algorithmSuite="Default" />
   </security>

在代码中,将代理类设置为允许模拟

proxy.ClientCredentials.Windows.AllowedImpersonationLevel =    
         System.Security.Principal.TokenImpersonationLevel.Impersonation;

您可以查看this post

hm2xizp9

hm2xizp92#

最后我不得不通过创建一个自定义端点并将其作为自定义端点行为添加到代理中来手动注入标头。

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
        }
    }

相关问题