how to add SOAP Security header

前端 未结 3 844
猫巷女王i
猫巷女王i 2021-02-04 16:08

I\'ve read a lot of articles and answers but I couldn\'t work it out.

I\'m using .NET Framework 4.0 on my project. So, I first added the WebService as a Service Referenc

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-04 16:29

    I have had the same issue as the First Attempt of the question and I needed to have a output same as the question. The answer is perfectly fine but Just to improve on the above answer or i should say combine the answer with the link i did the the following.

     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  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
        }
    }
    

提交回复
热议问题