how to add SOAP Security header

前端 未结 3 843
猫巷女王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:34

    I solved it by using WCF without any credentials declared. I did it by building my custom header I got help from this link

    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();
            }
        }
    

    and to how to use the header I got it from this link.

提交回复
热议问题