如何添加SOAP安全标头

它在

我已经阅读了很多文章和答案,但无法解决。

我在项目上使用.NET Framework 4.0。因此,我首先添加了WebService作为服务引用,并在我的app.config上获得了绑定。我将列出我的尝试


尝试#1

我像这样实例化了服务并添加了凭据:

在App.Config上

<binding name="SubscriptionWSImplServiceSoapBinding" >
  <security mode="TransportWithMessageCredential">
    <transport clientCredentialType="Basic"/>
  </security>
</binding>

SubscriptionWSImplServiceClient ClientWs = new SubscriptionWSImplServiceClient();
ClientWs.ClientCredentials.UserName.UserName = "some-username";
ClientWs.ClientCredentials.UserName.Password = "some-password";

var SomeResposne = ClientWs.RunMethod(); //I have exception at this point

我得到如下异常:

 System.ArgumentException: The provided URI scheme 'http' is invalid;
 expected 'https'. Parameter name: via at System.ServiceModel.Channels

例外情况是,Web服务的URL必须是HTTPS,但是给我的Web服务URL是HTTP而不是HTTPS。

但是我不介意HttpS,因此进行了第二次尝试。


尝试#2

该答案所述,我尝试如下:

BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
CustomBinding customBinding = new CustomBinding(binding);
SecurityBindingElement element = customBinding.Elements.Find<SecurityBindingElement>();
element.IncludeTimestamp = false;

EndpointAddress address = new EndpointAddress("https://www.GivenServiceUrl.com/WebService"); //Note that the url does not end as ..svc or ..asmx! Just the name like this with no dot or extension


SubscriptionWSImplServiceClient ClientWs = new SubscriptionWSImplService(customBinding, address);
ClientWs.ClientCredentials.UserName.UserName = "some-username";
ClientWs.ClientCredentials.UserName.Password = "some-password";

var SomeResposne = ClientWs.RunMethod(); //I have exception at this point

我得到如下异常:

System.ServiceModel.Security.SecurityNegotiationException:无法为具有权限“ https://www.GivenServiceUrl.com/WebService的SSL / TLS安全通道建立信任关系

因此,我进行了第三次尝试,在我的项目中我将其引用称为WebReference,而不是ServiceReference。


尝试#3

SubscriptionWSImplService ClientWs2 = new SubscriptionWSImplService();
var SomeResposne = ClientWs.RunMethod(); //I have exception at this point

我目前不知道如何发送安全标头。

我得到如下异常:

 System.Web.Services.Protocols.SoapHeaderException: An error was
 discovered processing the <wsse: Security> header

但是在这里,我无法添加必要的标题。你能帮我吗?


尝试1或尝试2或尝试3,考虑到我使用Framework 4.0,应该使用哪一个?我该如何解决这个问题?

根据文档,最终请求SOAP必须看起来像这样:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sub="http://subscription.services.ws.fourplay.com.tr/">
   <soapenv:Header>
      <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
         <wsse:UsernameToken wsu:Id="UsernameToken-12" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsse:Username>admin</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">123456</wsse:Password>
           <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">1lcj+WbCMlrPyhcud4QxiQ==</wsse:Nonce>
           <wsu:Created>2012-06-05T10:23:26.879Z</wsu:Created>
        </wsse:UsernameToken>
     </wsse:Security>
   </soapenv:Header>
   <soapenv:Body>
      <sub:RunMethod>
         <!--Optional:-->
         <sub:opaque id>555555555</sub:opaque id>
         <sub:params>
            <!--Optional:-->
            <name>GUID</name>
            <!--Optional:-->
            <value>2fc4ce1d-645e-41f4-811e-28510a02a17f </value>
         </sub:params>      
</sub: RunMethod >
它在

我通过使用WCF来解决它,而没有声明任何凭据。我通过构建自定义标题来做到这一点,我从此链接获得了帮助

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

以及如何使用标头,我是从这个链接中得到的

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何添加SOAP安全标头

来自分类Dev

SOAP安全标头和SOAP标头之间的区别

来自分类Dev

SOAP安全标头和SOAP标头之间的区别

来自分类Dev

将安全标头添加到 Xamarin.Forms 中的 SOAP Webservice

来自分类Dev

向请求添加SOAP标头

来自分类Dev

如何添加CSV标头

来自分类Dev

IRS Soap Fault-无效的WS安全标头

来自分类Dev

Soap API +设置安全标头作为响应

来自分类Dev

带有安全标头的 Android Retrofit soap 请求信封

来自分类Dev

如何在cxf soap请求中添加自定义标头?

来自分类Dev

如何从每个SOAP响应/请求抛出Java添加/读取自定义标头

来自分类Dev

如何将SOAP标头添加到肥皂消息-XSLT

来自分类Dev

如何在动态生成的Web服务中添加自定义SOAP标头?

来自分类Dev

如何在SOAP标头spring-ws中添加xmlbean文档元素

来自分类Dev

如何在 WCF 中向自动生成的代理添加soap 标头?

来自分类Dev

如何在jax-ws请求中添加安全标头

来自分类Dev

如何向Firebase托管应用程序添加安全标头

来自分类Dev

添加不带名称空间的SOAP标头元素

来自分类Dev

安全标头

来自分类Dev

如何向Android Picasso添加标头

来自分类Dev

如何使用AFNetworking添加请求标头?

来自分类Dev

如何使用AFNetworking添加请求标头?

来自分类Dev

如何用Ruby Savon编写SOAP Authentication标头

来自分类Dev

如何从SSRS XML DataService使用XMLDP发送SOAP标头

来自分类Dev

如何从SSRS XML DataService使用XMLDP发送SOAP标头

来自分类Dev

如何为脚本和样式标签添加随机数,以避免“不安全的内联” CSP标头字段?

来自分类Dev

SOAP 隐式标头

来自分类Dev

使用node.js中的soap模块在客户端中添加soap标头

来自分类Dev

使用node.js中的soap模块在客户端中添加soap标头

Related 相关文章

热门标签

归档