将WSE迁移到WCF

时间:2019-04-25 10:05:26

标签: .net wcf wse

 using Microsoft.Web.Services2;
 using Microsoft.Web.Services2.Security;
 using Microsoft.Web.Services2.Security.Tokens;
 using Microsoft.Web.Services2.Security.X509;

 public void ConfigureService(string username, string password, string certKey)
 {
        var service = new WebServicesClientProtocol();

        service.Url = url;

        service.RequestSoapContext.Security.Tokens.Add(new UsernameToken(username, password, PasswordOption.SendPlainText));

        var store = X509CertificateStore.LocalMachineStore(X509CertificateStore.MyStore);
        store.OpenRead();

        var cert = store.FindCertificateBySubjectString(certKey)[0];

        var clientToken = new X509SecurityToken(cert);

        store.Close();

        service.RequestSoapContext.Security.Tokens.Add(clientToken);

        var signature = new MessageSignature(clientToken);
        service.RequestSoapContext.Security.Elements.Add(signature);
}

这只是WebServicesClientProtocol的初始化,其余只是调用端点。

这是用于与服务通信的代码。是否可以仅使用WCF编写而不使用WSE?

我尝试了

            var binding = new BasicHttpsBinding(BasicHttpsSecurityMode.TransportWithMessageCredential);
        binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate;

        var address = new EndpointAddress(endpointAddress);
        var client = new ServiceSoapClient(binding, address);

        client.ClientCredentials.ClientCertificate.SetCertificate(
            StoreLocation.LocalMachine,
            StoreName.My,
            X509FindType.FindBySubjectName,
            certName);

        client.ClientCredentials.UserName.UserName = username
        client.ClientCredentials.UserName.Password = password;

但是它不起作用,我得到

System.Web.Services.Protocols.SoapException: Access to the operation is not present.
  at RecieversNamespace.Framework.ProcessMessage(SoapMessage message)
  at System.Web.Services.Protocols.SoapMessage.RunExtensions(SoapExtension[] extensions, Boolean throwOnException)
  at System.Web.Services.Protocols.SoapServerProtocol.CreateServerInstance()
  at System.Web.Services.Protocols.WebServiceHandler.Invoke()
  at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

System.Web.Services.Protocols.SoapException:不存在对该操作的访问。

添加服务端点时,我不确定如何设置绑定以复制上面的代码。特别是MessageSignature部分。

谢谢

0 个答案:

没有答案