首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在soap请求的标头中传递安全令牌

如何在soap请求的标头中传递安全令牌
EN

Stack Overflow用户
提问于 2019-07-25 13:31:44
回答 1查看 967关注 0票数 1

我希望按照下面的示例在c#代码的soap请求标头中传递安全令牌

我的服务url http://nclpwntapp10188.devaonnet.aon.net:9089/ECMGenericWS/v2/ECMGenericWS/ECMGenericWS.wsdl

代码语言:javascript
复制
<soapenv:Envelope xmlns:ecm="http://www.ECMGenericWebService.ecm.aon.com/ECMGenericWS/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" soapenv:mustUnderstand="1">
<wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="UsernameToken-8">
<wsse:Username>rrrrr</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">test</wsse:Password>
<wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">f8nUe3YupTU5ISdCy3X9Gg==</wsse:Nonce>
<wsu:Created>2011-05-04T19:01:40.981Z</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>

我尝试了下面的解决方案,但对我不起作用

How can I pass a username/password in the header to a SOAP WCF Service

EN

回答 1

Stack Overflow用户

发布于 2019-07-25 14:40:21

我确信有这样的库,但如果您可以访问原始XML并希望自己解析它,您将需要定义如下类:

代码语言:javascript
复制
    [XmlRoot(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        [XmlElement(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Header Header { get; set; }
    }

    public class Header
    {
        [XmlElement(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
        public Security Security { get; set; }
    }

    public class Security
    {
        [XmlAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public int mustUnderstand { get; set; }

        [XmlElement(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
        public UsernameToken UsernameToken { get; set; }
    }
    public class UsernameToken
    {
        [XmlAttribute(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")]
        public string Id { get; set; }

        public string Username { get; set; }

        public Password Password { get; set; }

        public Nonce Nonce { get; set; }

        [XmlElement(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")]
        public DateTime Created { get; set; }
    }

    public class Password
    {

        [XmlAttribute()]
        public string Type { get; set; }
        [XmlText]
        public string Value { get; set; }
    }

    public class Nonce
    {

        [XmlAttribute()]
        public string EncodingType { get; set; }
        [XmlText]
        public string Value { get; set; }
    }

此时,您可以使用XmlSerializer实例对其进行反序列化,例如:

代码语言:javascript
复制
const string xml = "...";
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
{
    var serializer = new XmlSerializer(typeof(Envelope));
    var envelope = (Envelope)serializer.Deserialize(stream);
    // Do stuff with Envelope, Security, UsernameToken, etc here...
}

=====

编辑: Harpreet要求提供完整的代码示例,因此如下所示。

注意,我还添加了示例</soapenv:Envelope>中缺失的结束XML标记。

代码语言:javascript
复制
using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;

// REF:
// How to pass security token in header of a soap request
// https://stackoverflow.com/questions/57194919/how-to-pass-security-token-in-header-of-a-soap-request/57195772

namespace Parse_security_token_in_header_of_a_soap_request
{
    [XmlRoot(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        [XmlElement(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Header Header { get; set; }
    }

    public class Header
    {
        [XmlElement(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
        public Security Security { get; set; }
    }

    public class Security
    {
        [XmlAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public int mustUnderstand { get; set; }

        [XmlElement(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
        public UsernameToken UsernameToken { get; set; }
    }
    public class UsernameToken
    {
        [XmlAttribute(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")]
        public string Id { get; set; }

        public string Username { get; set; }

        public Password Password { get; set; }

        public Nonce Nonce { get; set; }

        [XmlElement(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")]
        public DateTime Created { get; set; }
    }

    public class Password
    {

        [XmlAttribute()]
        public string Type { get; set; }
        [XmlText]
        public string Value { get; set; }
    }

    public class Nonce
    {

        [XmlAttribute()]
        public string EncodingType { get; set; }
        [XmlText]
        public string Value { get; set; }
    }

    class Program
    {
        const string xml =
            "<soapenv:Envelope xmlns:ecm=\"http://www.ECMGenericWebService.ecm.aon.com/ECMGenericWS/\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n" +
            "<soapenv:Header>\r\n" +
            "<wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" soapenv:mustUnderstand=\"1\">\r\n" +
            "<wsse:UsernameToken xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" wsu:Id=\"UsernameToken-8\">\r\n" +
            "<wsse:Username>rrrrr</wsse:Username>\r\n" +
            "<wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest\" >test</wsse:Password>\r\n" +
            "<wsse:Nonce EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\" >f8nUe3YupTU5ISdCy3X9Gg==</wsse:Nonce>\r\n" +
            "<wsu:Created>2011-05-04T19:01:40.981Z</wsu:Created>\r\n" +
            "</wsse:UsernameToken>\r\n" +
            "</wsse:Security>\r\n" +
            "</soapenv:Header>\r\n" +
            "</soapenv:Envelope>\r\n";

        static void Main(string[] args)
        {
            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
            {
                var serializer = new XmlSerializer(typeof(Envelope));
                var envelope = (Envelope)serializer.Deserialize(stream);
                // Do stuff with Envelope here...
                Console.WriteLine(envelope);
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57194919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档