http请求未经授权使用客户端身份验证方案' Anonymous'使用凭证身份时

时间:2018-06-12 21:22:12

标签: c# wcf soap

我尝试使用Visual Studio 2017中的WCF客户端生成器工具连接到SOAP服务。但是,当我发出请求时,我收到此错误:

  

" http请求未经授权使用客户端身份验证方案' Anonymous'"。

我尝试使用证书身份验证。这是我的App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
    <system.serviceModel>
        <diagnostics performanceCounters="Default" />
        <behaviors>
            <endpointBehaviors>
                <behavior name="NewBehavior0">
                    <clientCredentials useIdentityConfiguration="true">
                        <clientCertificate findValue="portalservicos.jucemg.mg.gov.br"
                            storeLocation="LocalMachine" storeName="TrustedPeople" x509FindType="FindByIssuerName" />
                        <serviceCertificate>
                            <authentication certificateValidationMode="PeerOrChainTrust"
                                trustedStoreLocation="LocalMachine" />
                        </serviceCertificate>
                    </clientCredentials>
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <bindings>
            <basicHttpBinding>
                <binding name="NewBinding2">
                    <security mode="Transport">
                        <transport clientCredentialType="Certificate" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="https://newsrm.jucemg.mg.gov.br:443/empreendimento/service/ViabilidadePrefeituraWS"
                behaviorConfiguration="NewBehavior0" binding="basicHttpBinding"
                bindingConfiguration="NewBinding2" contract="ViabilidadePrefeituraWS.ViabilidadePrefeituraWS"
                name="ViabilidadePrefeituraWSPort">
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

这是我的代码:

class Program
{
    static void Main(string[] args)
    {
        var ws = new ViabilidadePrefeituraWSClient();
        ws.callService("");
    }
}

当我在那里设置断点时,我看到证书确实正在加载(ws.ClientCredentials.ClientCertificate显示正确的证书)。但不知何故,似乎没有发送证书,或者配置以某种方式未被使用,因为错误提到了身份验证方案&#34; Anonymous&#34;。

我看到了这个问题(WCFTestClient The HTTP request is unauthorized with client authentication scheme 'Anonymous')和其他类似的问题,但是没有一个是关于使用证书进行身份验证的。

0 个答案:

没有答案