Как использовать веб-сервис WSDL с использованием сертификата хранилища ключей Pkcs12 - PullRequest
2 голосов
/ 13 февраля 2020

Мне нужно использовать сторонний веб-сервис. У меня есть файл WSDL и файл .pkcs12 Keystore и пароль. Используя этот файл WSDL, я добавил веб-ссылку в проект. прочитайте файл Keystore. Создан новый экземпляр класса X509certificate2 и импортирован сертификат после добавления в класс обслуживания. Я пытаюсь вызвать метод в моем сервисе

            service.mymethod(param1)--> (At this line its throwing error stating that ws-security header not found)

, прибегнув к поиску ошибки, которую я нашел StackOverflow ссылка для добавления заголовка безопасности после перехода по этой ссылке здесь полный код

                //reading PCKS12 certificate
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                var path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Content\\myKeyStoreFile.pkcs12");
                var data = System.IO.File.ReadAllBytes(path);
                //Importing Certificate
                X509Certificate2 certificate = new X509Certificate2();
                certificate.Import(data, "password", X509KeyStorageFlags.DefaultKeySet);
                //adding WS-Security Headers
                UsernameToken token = new UsernameToken("keyname", "password", PasswordOption.SendHashed);
                service.RequestSoapContext.Security.Tokens.Add(token);
                //adding certificate to service
                service.ClientCertificates.Add(certificate);
                //calling proxy class(service method)
                service.methodname(param1);-->(its throwing System.web.service.protocols.soapheaderexception:'nested exception is org.apache.wss4j.common.ext.WSSSecurityException Original Exception was javax.security.auth.callback.unsupportedcallbackexception)

У меня есть java код (реализован в весенней загрузке) для справки. Wss4jSecurityInterceptor securityInterceptor = new Wss4jSecurityInterceptor ();

    //crypto varible contains .pkcs12 file path and password properties
    Crypto crypto = null;
    try {
        crypto = CryptoFactory.getInstance(cryptoPropertyFile);
    }catch(WSSecurityException e) {
        e.printStackTrace();}
    securityInterceptor.setSecurementActions("Encrypt Signature");
    securityInterceptor.setSecurementEncryptionUser(trustedCertKeyAlias);
    securityInterceptor.setSecurementEncryptionCrypto(crypto);
    securityInterceptor.setSecurementEncryptionParts("{Content {http://schemas.xmlsoap.org/soap/envelope/}Body");
    securityInterceptor.setSecurementUsername(privateKeyAlias);
    securityInterceptor.setSecurementPassword(privateKeyPassword);
    securityInterceptor.setSecurementSignatureCrypto(crypto);
securityInterceptor.setSecurementSignatureKeyIdentifier("DirectReference");
    securityInterceptor.setSecurementSignatureUser(privateKeyAlias);
    securityInterceptor.setSecurementSignatureParts("{Content}{http://schemas.xmlsoap.org/soap/envelope/}Body");        
    securityInterceptor.setValidationActions("Encrypt");
    securityInterceptor.setValidationDecryptionCrypto(crypto);
    KeyStoreCallbackHandler keyStoreCallbackHandler = new KeyStoreCallbackHandler();
    keyStoreCallbackHandler.setPrivateKeyPassword(privateKeyPassword);
securityInterceptor.setValidationCallbackHandler(keyStoreCallbackHandler);
    LogHttpHeaderClientInterceptor logHttpHeaderClientInterceptor = new LogHttpHeaderClientInterceptor();
    ClientInterceptor[] interceptors = {securityInterceptor, logHttpHeaderClientInterceptor};
    template.setInterceptors(interceptors);

Может кто-нибудь, пожалуйста, дайте мне знать, как добавить перехватчики в do tnet. Я провел некоторое исследование, но не смог найти никакого решения. Есть ли что-то, что смазывает Wss4jSecurityInterceptor в do tnet.

1 Ответ

3 голосов
/ 15 марта 2020

IClientMessageInspector может быть чем-то, что вам нужно.

Вам нужно создать IEndpointBehavior и добавить IClientMessageInspector к своему поведению, затем добавить поведение к конечной точке, используемой для создания ChannelFactory.

См .: https://docs.microsoft.com/en-us/dotnet/framework/wcf/extending/how-to-inspect-or-modify-messages-on-the-client

См. Также: https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.dispatcher.iclientmessageinspector?view=netframework-4.8

Пример:

class MyEndpointBehavior : IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        clientRuntime.ClientMessageInspectors.Add(new MyMessageInspector());
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
    }

    public void Validate(ServiceEndpoint endpoint)
    {
    }
}

class MyMessageInspector : IClientMessageInspector
{
    public void AfterReceiveReply(ref Message reply, object correlationState)
    {
    }

    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        return null;
    }
}

Использовать инспектор сообщений:

var endpoint = new EndpointAddress("<your webservice uri>");
var binding = new BasicHttpBinding(); // Assume you are using HTTP binding
var channelFactory = new ChannelFactory<Soap>(binding, endpoint);
channelFactory.Endpoint.EndpointBehaviors.Add(new MyEndpointBehavior());
var client = channelFactory.CreateChannel();
...