首页 > 解决方案 > 使用凭证身份时,http 请求未经客户端身份验证方案“匿名”授权

问题描述

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

“使用客户端身份验证方案‘匿名’的 http 请求未经授权”。

我正在尝试使用证书身份验证。这是我的 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显示正确的证书)。但不知何故,似乎没有发送证书,或者没有使用配置,因为错误提到了身份验证方案“匿名”。

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

标签: c#wcfsoap

解决方案


推荐阅读