c# - 使用凭证身份时,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')和其他类似问题,但是没有一个是关于使用证书进行身份验证的。
解决方案
推荐阅读
- tensorflow - TensorRT 如何处理数据?
- python - 使 Spark 结构化流中的 JSON 可以在 python (pyspark) 中作为没有 RDD 的数据帧访问
- angular - Webpack Live Reload 总是落后一步
- javascript - 强制滚动 div 元素停止
- unity3d - 如何在 Unity 项目之间传输数据
- angular - 在角度分量之间使用静态数据
- perl - 如何在 Perl 中从 XML 文件中获取数据?
- javascript - 来自设备的 JSON 格式不正确
- unity3d - 当尝试在 Unity 中使用 Mapbox SDK 导出项目的 APK 时,它会崩溃 IL2CPP 错误
- node.js - 防止同一用户同时登录不同系统