WCF 将 app.config 迁移到代码隐藏原因 传出消息的身份检查失败 工作客户端APP.CONFIG客户端代码隐藏服务实现

问题描述

我需要使用 WCF 服务,但我需要将客户端配置从 app.config 移动到代码隐藏。 我制作了 POC,并且在使用 app.config 文件时正在工作,但是当我尝试从代码隐藏中“模仿”配置时 我收到错误

发消息的身份检查失败。 预期的身份是“身份 (http://schemas.xmlsoap.org/ws/2005/05/identity/right/possesspropertyhttp://schemas.xmlsoap.org/ws/2005/05/identity/claims/thumbprint)' 对于端点

代码隐藏中的这一行相当于什么?

        <identity>
          <certificate encodedValue="{here is my key}"/>
        </identity>

工作客户端APP.CONfig

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="ServiceEndPoint" transactionFlow="true">
          <security>
            <message clientCredentialType="Certificate" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    
    <client>
      <endpoint address="http://localhost/Messaging/SmsService.svc"
          binding="wsHttpBinding" bindingConfiguration="ServiceEndPoint"
          contract="ServiceReference.ISmsService" name="ServiceEndPoint" behaviorConfiguration="CustomBehavior">
        <identity>
          <certificate encodedValue="{here is my key}"/>
        </identity>
      </endpoint>
    </client>

    <behaviors>
      <endpointBehaviors>
        <behavior name="CustomBehavior">
          <clientCredentials>

            <serviceCertificate>
              <authentication certificateValidationMode="PeerTrust"/>
            </serviceCertificate>

            <clientCertificate findValue="DaExAgentClient" x509FindType="FindBySubjectName" storeLocation="LocalMachine" storeName="TrustedPeople" />

          </clientCredentials>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

客户端代码隐藏

这是我当前在代码隐藏中的实现 app.config

WSHttpBinding binding = new WSHttpBinding(SecurityMode.Message,true);  
//binding.SendTimeout = TimeSpan.FromMinutes(1);
//binding.OpenTimeout = TimeSpan.FromMinutes(1);
//binding.CloseTimeout = TimeSpan.FromMinutes(1);
//binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
//binding.AllowCookies = false;
//binding.BypassproxyOnLocal = false;
//binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
//binding.MessageEncoding = WSMessageEncoding.Text;
//binding.TextEncoding = Encoding.UTF8;
//binding.UseDefaultWebProxy = true;

binding.TransactionFlow = true;

binding.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
//binding.Security.Mode = SecurityMode.Message;


X509Certificate2 cert = new X509Certificate2(@"C:\DaExAgentClient.crt");

var bytes = cert.Export(X509ContentType.SerializedCert);
string s = Convert.ToBase64String(bytes);

EndpointIdentity identity = EndpointIdentity.CreateX509CertificateIdentity(cert);

EndpointAddress endpoint = new EndpointAddress(new Uri("http://localhost/Messaging/SmsService.svc"),identity);

SmsServiceClient clientProxy = new SmsServiceClient(binding,endpoint);
clientProxy.ClientCredentials.ClientCertificate.SetCertificate(
  StoreLocation.LocalMachine,StoreName.TrustedPeople,X509FindType.FindBySubjectName,"DaExAgentClient");

clientProxy.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.PeerTrust;


using (TransactionScope scope = new TransactionScope(TransactionScopeOption.required,TimeSpan.FromMinutes(2)))
{
    string hw = clientProxy.HelloWorld();
    scope.Complete();
}


clientProxy.Close(); 

服务实现

[ServiceContract(SessionMode = SessionMode.required)]
public interface ISmsService
{
    [OperationContract(IsOneWay = false)]
    [TransactionFlow(TransactionFlowOption.Mandatory)]
    string HelloWorld();
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)