Saltar al contenido

cómo agregar encabezado de seguridad SOAP

Nuestro grupo de especialistas pasados algunos días de investigación y recopilación de de información, obtuvimos los datos necesarios, queremos que te sea útil en tu trabajo.

Solución:

Lo resolví usando WCF sin ninguna credencial declarada. Lo hice construyendo mi encabezado personalizado. Obtuve ayuda de este enlace.

public class SoapSecurityHeader : MessageHeader
    
        private readonly string _password, _username, _nonce;
        private readonly DateTime _createdDate;

        public SoapSecurityHeader(string id, string username, string password, string nonce)
        
            _password = password;
            _username = username;
            _nonce = nonce;
            _createdDate = DateTime.Now;
            this.Id = id;
        

        public string Id  get; set; 

        public override string Name
        
            get  return "Security"; 
        

        public override string Namespace
        
            get  return "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"; 
        

        protected override void OnWriteStartHeader(XmlDictionaryWriter writer, MessageVersion messageVersion)
        
            writer.WriteStartElement("wsse", Name, Namespace);
            writer.WriteXmlnsAttribute("wsse", Namespace);
        

        protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
        
            writer.WriteStartElement("wsse", "UsernameToken", Namespace);
            writer.WriteAttributeString("Id", "UsernameToken-10");
            writer.WriteAttributeString("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");

            writer.WriteStartElement("wsse", "Username", Namespace);
            writer.WriteValue(_username);
            writer.WriteEndElement();

            writer.WriteStartElement("wsse", "Password", Namespace);
            writer.WriteAttributeString("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
            writer.WriteValue(_password);
            writer.WriteEndElement();

            writer.WriteStartElement("wsse", "Nonce", Namespace);
            writer.WriteAttributeString("EncodingType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
            writer.WriteValue(_nonce);
            writer.WriteEndElement();

            writer.WriteStartElement("wsse", "Created", Namespace);
            writer.WriteValue(_createdDate.ToString("YYYY-MM-DDThh:mm:ss"));
            writer.WriteEndElement();

            writer.WriteEndElement();
        
    

y cómo usar el encabezado lo obtuve de este enlace.

Tuve el mismo problema que el primer intento de la pregunta y necesitaba tener un resultado igual al de la pregunta. La respuesta está perfectamente bien, pero solo para mejorar la respuesta anterior o debería decir que combine la respuesta con el enlace, hice lo siguiente.

 public class Security : MessageHeader

    private readonly string _password, _username, _nonce;
    private readonly DateTime _createdDate;

    public Security(string id, string username, string password, string nonce)
    
        _password = password;
        _username = username;
        _nonce = nonce;
        _createdDate = DateTime.Now;
        this.Id = id;
    

    public string Id  get; set; 

    public override string Name => "Security";

    public override string Namespace => "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";

    protected override void OnWriteStartHeader(XmlDictionaryWriter writer, MessageVersion messageVersion)
    
        writer.WriteStartElement("wsse", Name, Namespace);
        writer.WriteXmlnsAttribute("wsse", Namespace);
    

    protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
    
        writer.WriteStartElement("wsse", "UsernameToken", Namespace);
        writer.WriteAttributeString("Id", "UsernameToken-10");
        writer.WriteAttributeString("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");

        writer.WriteStartElement("wsse", "Username", Namespace);
        writer.WriteValue(_username);
        writer.WriteEndElement();

        writer.WriteStartElement("wsse", "Password", Namespace);
        writer.WriteAttributeString("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
        writer.WriteValue(_password);
        writer.WriteEndElement();

        writer.WriteStartElement("wsse", "Nonce", Namespace);
        writer.WriteAttributeString("EncodingType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
        writer.WriteValue(_nonce);
        writer.WriteEndElement();

        writer.WriteStartElement("wsse", "Created", Namespace);
        writer.WriteValue(_createdDate.ToString("YYYY-MM-DDThh:mm:ss"));
        writer.WriteEndElement();

        writer.WriteEndElement();
    

/// 
/// Represents a message inspector object that can be added to the MessageInspectors collection to view or modify messages.
/// 
public class ClientMessageInspector : IClientMessageInspector

    /// 
    /// Enables inspection or modification of a message before a request message is sent to a service.
    /// 
    /// The message to be sent to the service.
    /// The WCF client object channel.
    /// 
    /// The object that is returned as the  argument of
    /// the [email protected],System.Object)" /> method.
    /// This is null if no correlation state is used.The best practice is to make this a  to ensure that no two
    ///  objects are the same.
    /// 
    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    
///enter your username and password here.
        Security header = new Security("xx", "xx", "xx!","xx");

        request.Headers.Add(header);
        return header.Name;
    

    /// 
    /// Enables inspection or modification of a message after a reply message is received but prior to passing it back to the client application.
    /// 
    /// The message to be transformed into types and handed back to the client application.
    /// Correlation state data.
    public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
    

    


/// 
/// Represents a run-time behavior extension for a client endpoint.
/// 
public class CustomEndpointBehavior : IEndpointBehavior

    /// 
    /// Implements a modification or extension of the client across an endpoint.
    /// 
    /// The endpoint that is to be customized.
    /// The client runtime to be customized.
    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    
        clientRuntime.ClientMessageInspectors.Add(new ClientMessageInspector());
    

    /// 
    /// Implement to pass data at runtime to bindings to support custom behavior.
    /// 
    /// The endpoint to modify.
    /// The objects that binding elements require to support the behavior.
    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    
        // Nothing special here
    

    /// 
    /// Implements a modification or extension of the service across an endpoint.
    /// 
    /// The endpoint that exposes the contract.
    /// The endpoint dispatcher to be modified or extended.
    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    
        // Nothing special here
    

    /// 
    /// Implement to confirm that the endpoint meets some intended criteria.
    /// 
    /// The endpoint to validate.
    public void Validate(ServiceEndpoint endpoint)
    
        // Nothing special here
    

Excelente puesto. ¡¡Todos ustedes salvan mi vida !! Tuve que hacer algunos cambios para obtener los siguientes encabezados:


    
        EFO140714JPA
        SgBJAHMANwBBACYANQBOAG8ANwAzACEANgBrAGEAJgBIAGwAJABMAA==
        SgBJAHMANwBBACYANQBOAG8ANwAzACEANgBrAGEAJgBIAGwAJABMAA==
        2016-08-04T11:24:10.0Z
    

Los cambios que hice son los siguientes:

        public class SoapSecurityHeader : MessageHeader
        
            private readonly string _password, _username, _nonce, _createdDate;

            public SoapSecurityHeader(string id, string username, string password, string nonce, string created)
            
                _password = password;
                _username = username;
                _nonce = nonce;
                _createdDate = created;
                this.Id = id;
            

            public string Id  get; set; 

            public override string Name
            
                get  return "Security"; 
            

            public override string Namespace
            
                get  return "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"; 
            

            protected override void OnWriteStartHeader(XmlDictionaryWriter writer, MessageVersion messageVersion)
            
                writer.WriteStartElement("wsse", Name, Namespace);
                writer.WriteXmlnsAttribute("wsse", Namespace);
 ------->       writer.WriteXmlAttribute("mustUnderstand", "1");
            


            protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
            
                writer.WriteStartElement("wsse", "UsernameToken", Namespace);
                writer.WriteAttributeString("wsu:Id", "UsernameToken-15");
                writer.WriteAttributeString("xmlns:wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");

                writer.WriteStartElement("wsse", "Username", Namespace);
                writer.WriteValue(_username);
                writer.WriteEndElement();

                writer.WriteStartElement("wsse", "Password", Namespace);
  ----->        writer.WriteAttributeString("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest");
                writer.WriteValue(_password);
                writer.WriteEndElement();

                writer.WriteStartElement("wsse", "Nonce", Namespace);
                writer.WriteValue(_nonce);
                writer.WriteEndElement();

  ------>       writer.WriteStartElement("wsu:Created");
                writer.WriteValue(_createdDate);
                writer.WriteEndElement();

                writer.WriteEndElement();
            
        

Si sostienes alguna duda y capacidad de avanzar nuestro reseña puedes realizar una interpretación y con mucho gusto lo observaremos.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *