消耗Web服务:客户端应用程序:C#

时间:2018-11-04 09:13:41

标签: c# wcf soap ws-security

错误:

  

“ System.ServiceModel.FaultException:InvalidSecurity:错误   正在处理WS-Security安全标头” ...

任何人都可以在这个问题上提供帮助

   public void CallServiceMNB()
        {
            ServiceReference1.SOAPCLIENT objMNB = new ServiceReference1.SOAPCLIENT();
            ServiceReference1.SOAPCLIENTinput input = new ServiceReference1.SOAPCLIENTinput();
            string Username = "xxx";
            string Password = "xxxx";
            objMNB.ClientCredentials.UserName.UserName = Username;
            objMNB.ClientCredentials.UserName.Password = Password;
            input.p_FROM_DATE = "01-01-2018";
            input.p_JE_CATEGORY = "xxxx";
            input.p_GROUP_ID = "";
            input.p_JE_SOURCE = "YYYYYY";
            input.p_LEDGER_NAME = "YYYYY";
            input.p_TO_DATE = "31-12-2018";
            try
            {
                objMNB.Open();
                objMNB.GetGLDetails(input);
                objMNB.Close();
            }
            catch (Exception Ex)
            {
                this.richTextBox1.Text = Ex.ToString();
                objMNB.Close();
            }
        }


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
    <system.serviceModel>
        <bindings>
            <customBinding>
                <binding name="YYYYYY">
                    <!--    WsdlImporter encountered unrecognized policy assertions in ServiceDescription 'http://gl.ws.apps.oracle.xxdof/':    -->
                    <!--    <wsdl:binding name='YYYYYY'>    -->
                    <!--        <sp:SupportingTokens xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">..</sp:SupportingTokens> -->




                    <textMessageEncoding messageVersion="Soap11" />
                    <httpsTransport />


                </binding>
            </customBinding>
        </bindings>
        <client>
            <endpoint address="https://XXXXX"
                binding="customBinding" bindingConfiguration="YYYYYY"
                contract="ServiceReference1.SOAPCLIENT" name="SOAPCLIENT" />
        </client>
    </system.serviceModel>
</configuration>

1 个答案:

答案 0 :(得分:1)

客户端凭据取决于在服务器上的“自定义绑定”托管中创建的SecurityBindingElement。
https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.channels.securitybindingelement?view=netframework-4.7.2
您应该根据自定义绑定的身份验证安全模式将令牌添加到安全标头中。
对于BasicHttpBinding,默认情况下不需要提供凭据,而默认情况下我们需要在Wshttpbinding中提供Windows帐户。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/programming-wcf-security