有次我们有个项目需要Call 一个 Java 的 web service, Soap包中需要一个 Security Head

  1. <soapenv:Header>
  2. <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
  3. <wsse:UsernameToken xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" wsu:Id="UsernameToken-1" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
  4. <wsse:Username>username</wsse:Username>
  5. <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password>
  6. </wsse:UsernameToken>
  7. </wsse:Security>
  8. </soapenv:Header>

但是.net 默认的 Credentials 添加的 UserName 不符合这种格式

  1. orgClient.ClientCredentials.UserName.UserName = "userName";
  2. orgClient.ClientCredentials.UserName.Password = "password";

所以总是报错

System.Web.Services.Protocols.SoapHeaderException: An error was

discovered processing the <wsse: Security> header

没奈何,就只有用力气活,手动的把这段WSSE 的 head 添加到 Soap 包里面去了。

  1. orgClient.Endpoint.EndpointBehaviors.Add(new CustomEndpointBehavior());

下面是Behavior

  1. /// <summary>
  2.     /// Represents a run-time behavior extension for a client endpoint.
  3.     /// </summary>
  4.     public class CustomEndpointBehavior : IEndpointBehavior
  5.     {
  6.         /// <summary>
  7.         /// Implements a modification or extension of the client across an endpoint.
  8.         /// </summary>
  9.         /// <param name="endpoint">The endpoint that is to be customized.</param>
  10.         /// <param name="clientRuntime">The client runtime to be customized.</param>
  11.         public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
  12.         {
  13.             clientRuntime.ClientMessageInspectors.Add(new ClientMessageInspector());
  14.         }
  15.  
  16.         /// <summary>
  17.         /// Implement to pass data at runtime to bindings to support custom behavior.
  18.         /// </summary>
  19.         /// <param name="endpoint">The endpoint to modify.</param>
  20.         /// <param name="bindingParameters">The objects that binding elements require to support the behavior.</param>
  21.         public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
  22.         {
  23.             // Nothing special here
  24.         }
  25.  
  26.         /// <summary>
  27.         /// Implements a modification or extension of the service across an endpoint.
  28.         /// </summary>
  29.         /// <param name="endpoint">The endpoint that exposes the contract.</param>
  30.         /// <param name="endpointDispatcher">The endpoint dispatcher to be modified or extended.</param>
  31.         public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
  32.         {
  33.             // Nothing special here
  34.         }
  35.  
  36.         /// <summary>
  37.         /// Implement to confirm that the endpoint meets some intended criteria.
  38.         /// </summary>
  39.         /// <param name="endpoint">The endpoint to validate.</param>
  40.         public void Validate(ServiceEndpoint endpoint)
  41.         {
  42.             // Nothing special here
  43.         }
  44.     }

请注意13行红色部分的代码,相当于一层层的使用了Provider 的模式

  1. /// <summary>
  2.     /// Represents a message inspector object that can be added to the <c>MessageInspectors</c> collection to view or modify messages.
  3.     /// </summary>
  4.     public class ClientMessageInspector : IClientMessageInspector
  5.     {
  6.         /// <summary>
  7.         /// Enables inspection or modification of a message before a request message is sent to a service.
  8.         /// </summary>
  9.         /// <param name="request">The message to be sent to the service.</param>
  10.         /// <param name="channel">The WCF client object channel.</param>
  11.         /// <returns>
  12.         /// The object that is returned as the <paramref name="correlationState " /> argument of
  13.         /// the <see cref="M:System.ServiceModel.Dispatcher.IClientMessageInspector.AfterReceiveReply(System.ServiceModel.Channels.Message@,System.Object)" /> method.
  14.         /// This is null if no correlation state is used.The best practice is to make this a <see cref="T:System.Guid" /> to ensure that no two
  15.         /// <paramref name="correlationState" /> objects are the same.
  16.         /// </returns>
  17.         public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)
  18.         {
  19.             SoapSecurityHeader header = new SoapSecurityHeader("UsernameToken-1", UserName, Password, "");
  20.  
  21.             request.Headers.Add(header);
  22.  
  23.             return header.Id;
  24.         }
  25.  
  26.         /// <summary>
  27.         /// Enables inspection or modification of a message after a reply message is received but prior to passing it back to the client application.
  28.         /// </summary>
  29.         /// <param name="reply">The message to be transformed into types and handed back to the client application.</param>
  30.         /// <param name="correlationState">Correlation state data.</param>
  31.         public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
  32.         {
  33.             var a = reply;
  34.             // Nothing special here
  35.         }
  36.     }

下面是写入Head的部分

  1. public class SoapSecurityHeader : MessageHeader
  2.     {
  3.         private readonly string _password, _username, _nonce;
  4.         private readonly DateTime _createdDate;
  5.  
  6.         public SoapSecurityHeader(string id, string username, string password, string nonce)
  7.         {
  8.             _password = password;
  9.             _username = username;
  10.             _nonce = nonce;
  11.             _createdDate = DateTime.Now;
  12.             this.Id = id;
  13.         }
  14.  
  15.         public string Id { get; set; }
  16.  
  17.         public override string Name
  18.         {
  19.             get { return "Security"; }
  20.         }
  21.  
  22.         public override string Namespace
  23.         {
  24.             get { return "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"; }
  25.         }
  26.  
  27.         protected override void OnWriteStartHeader(XmlDictionaryWriter writer, MessageVersion messageVersion)
  28.         {
  29.             writer.WriteStartElement("wsse", Name, Namespace);
  30.             writer.WriteXmlnsAttribute("wsse", Namespace);
  31.         }
  32.  
  33.         protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
  34.         {
  35.             writer.WriteStartElement("wsse", "UsernameToken", Namespace);
  36.             writer.WriteAttributeString("Id", "UsernameToken-10");
  37.             writer.WriteAttributeString("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
  38.  
  39.             writer.WriteStartElement("wsse", "Username", Namespace);
  40.             writer.WriteValue(_username);
  41.             writer.WriteEndElement();
  42.  
  43.             writer.WriteStartElement("wsse", "Password", Namespace);
  44.             writer.WriteAttributeString("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
  45.             writer.WriteValue(_password);
  46.             writer.WriteEndElement();
  47.  
  48.             writer.WriteStartElement("wsse", "Nonce", Namespace);
  49.             writer.WriteAttributeString("EncodingType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
  50.             writer.WriteValue(_nonce);
  51.             writer.WriteEndElement();
  52.  
  53.             writer.WriteStartElement("wsse", "Created", Namespace);
  54.             writer.WriteValue(_createdDate.ToString("YYYY-MM-DDThh:mm:ss"));
  55.             writer.WriteEndElement();
  56.  
  57.             writer.WriteEndElement();
  58.         }
  59.     }

至此大功告成!

扩展Wcf call security service, 手动添加 Soap Security Head.的更多相关文章

  1. WCF 在VS中,添加服务引用,地址输入http://ip/Service.svc,点击前往,提示错误,内容如下:

    WCF的service端的webconfig如下: <?xml version="1.0"?> <configuration> <system.ser ...

  2. centos6.5 gsoap安装过程+ php添加soap扩展

    参考博客: CentOS编译安装gSOAP Linux C实现webservice调用 安装gsoap流程  里面提到make时可能碰到的问题 还没有用到 1.从官网下载最新的版本:http://so ...

  3. wcf和web service的区别

    1.WebService:严格来说是行业标准,不是技术,使用XML扩展标记语言来表示数据(这个是夸语言和平台的关键).微软的Web服务实现称为ASP.NET Web Service.它使用Soap简单 ...

  4. WCF和Web Service的 区(guan)别(xi)

    参考文献:http://social.microsoft.com/Forums/zh-CN/c06420d1-69ba-4aa6-abe5-242e3213b68f/wcf-webservice 之前 ...

  5. WCF与 Web Service的区别是什么?各自的优点在哪里呢?

    这是很多.NET开发人员容易搞错的问题.面试的时候也经常遇到,初学者也很难分快速弄明白 Web service: .net技术中其实就指ASP.NET Web Service,用的时间比较长,微软其实 ...

  6. 如何手动添加Windows服务和如何把一个服务删除

    windows 手动添加服务方法一:修改注册表 在注册表编辑器,展开分支"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services" ...

  7. Windows服务的手动添加和删除方法

    Windows服务的手动添加和删除方法 服务,是指执行指定系统功能的程序.例程或进程,以便支持其他程序,尤其是低层(接近硬件)程序.其实,服务就是一种特殊的应用程序,它从服务启动开始就一直处于运行状态 ...

  8. 解决 Cocos2d-x 中 Android.mk 手动添加源文件

    转自:http://blog.csdn.net/ypfsoul/article/details/8909178 Makefile Android.mk 引发的思索 在我们编写 Android 平台 c ...

  9. Quartz动态添加定时任务执行sql(服务启动添加+手动添加)

    系统用来每天插入视图数据... 一.数据库表设计 1.接口配置表(t_m_db_interface_config) 2.接口日志表(t_m_db_interface_log) 3.前端配置页面 查询页 ...

随机推荐

  1. ios 单例设计模式

    单例模式的意思就是只有一个实例.单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例.这个类称为单例类.单例可用性非常高,用于登录用户管理等可供全局调用. + (AccountMa ...

  2. Daily Scrum02 12.08

    编译大作业的第一次检查终于过去了,已经经过这次检查的组员们可以暂时松一口气了. 也希望编译大作业有着落的成员可以多花一些时间在团队任务上,帮其他的组员多分担一些工作. 第一次没来的及检查的同学,或是没 ...

  3. css 设置圆角

    CSS3 圆角(border-radius) -moz(例如 -moz-border-radius)用于Firefox -webkit(例如:-webkit-border-radius)用于Safar ...

  4. Java集合类源码学习- Iterabel<T>,Colection<E>,AbstractCollection<E>

    Collection<E>接口extends Iteratable<E>接口. Iteratable<T>:实现接口使得对象能够成为“for-each loop”的 ...

  5. requirejs:研究笔记

    模块化历史 模块化异步加载方式 后期维护 查找问题 复用代码 防止全局变量的污染 http://requirejs.cn/ http://requirejs.org/ 我的目录结构 总体步骤 < ...

  6. java并发容器类

    本文主要介绍java并发容器相关实现类,collections节点下接口方法介绍. Queue Java提供的线程安全的Queue可以分为阻塞队列和非阻塞队列,其中阻塞队列的典型例子是Blocking ...

  7. Python 随机数生成总结

    random.uniform(a, b),返回[a,b]之间的浮点数 random.randint(a, b),返回[a,b]之间的整数 random.randrange([start], stop[ ...

  8. Sony Z1 flashtool 刷机笔记

    第一次硬刷,(相较于recovery的卡刷)差点变成无限重启..记录一些关键步骤: 1 unlock bootloader http://developer.sonymobile.com/unlock ...

  9. Xshell远程连接工具

    下载地址:http://rj.baidu.com/soft/detail/15201.html?ald Xshell 是一个强大的安全终端模拟软件,它支持SSH1, SSH2, 以及Microsoft ...

  10. ncurses库的一些函数

    为了实现一个简单的聊天程序,如果使用普通的输入输出函数,会很凌乱.so,便想着能不能用下 ncurses这个字符图形库 总结一下,就是这样. 使用ncurses时,先需要初始化窗口,程序结束时,主动调 ...