扩展Wcf call security service, 手动添加 Soap Security Head.
有次我们有个项目需要Call 一个 Java 的 web service, Soap包中需要一个 Security Head
- <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 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">
- <wsse:Username>username</wsse:Username>
- <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">password</wsse:Password>
- </wsse:UsernameToken>
- </wsse:Security>
- </soapenv:Header>
但是.net 默认的 Credentials 添加的 UserName 不符合这种格式
- orgClient.ClientCredentials.UserName.UserName = "userName";
- orgClient.ClientCredentials.UserName.Password = "password";
所以总是报错
System.Web.Services.Protocols.SoapHeaderException: An error was
discovered processing the <wsse: Security> header
没奈何,就只有用力气活,手动的把这段WSSE 的 head 添加到 Soap 包里面去了。
- orgClient.Endpoint.EndpointBehaviors.Add(new CustomEndpointBehavior());
下面是Behavior
- /// <summary>
- /// Represents a run-time behavior extension for a client endpoint.
- /// </summary>
- public class CustomEndpointBehavior : IEndpointBehavior
- {
- /// <summary>
- /// Implements a modification or extension of the client across an endpoint.
- /// </summary>
- /// <param name="endpoint">The endpoint that is to be customized.</param>
- /// <param name="clientRuntime">The client runtime to be customized.</param>
- public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
- {
- clientRuntime.ClientMessageInspectors.Add(new ClientMessageInspector());
- }
- /// <summary>
- /// Implement to pass data at runtime to bindings to support custom behavior.
- /// </summary>
- /// <param name="endpoint">The endpoint to modify.</param>
- /// <param name="bindingParameters">The objects that binding elements require to support the behavior.</param>
- public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
- {
- // Nothing special here
- }
- /// <summary>
- /// Implements a modification or extension of the service across an endpoint.
- /// </summary>
- /// <param name="endpoint">The endpoint that exposes the contract.</param>
- /// <param name="endpointDispatcher">The endpoint dispatcher to be modified or extended.</param>
- public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
- {
- // Nothing special here
- }
- /// <summary>
- /// Implement to confirm that the endpoint meets some intended criteria.
- /// </summary>
- /// <param name="endpoint">The endpoint to validate.</param>
- public void Validate(ServiceEndpoint endpoint)
- {
- // Nothing special here
- }
- }
请注意13行红色部分的代码,相当于一层层的使用了Provider 的模式
- /// <summary>
- /// Represents a message inspector object that can be added to the <c>MessageInspectors</c> collection to view or modify messages.
- /// </summary>
- public class ClientMessageInspector : IClientMessageInspector
- {
- /// <summary>
- /// Enables inspection or modification of a message before a request message is sent to a service.
- /// </summary>
- /// <param name="request">The message to be sent to the service.</param>
- /// <param name="channel">The WCF client object channel.</param>
- /// <returns>
- /// The object that is returned as the <paramref name="correlationState " /> argument of
- /// the <see cref="M:System.ServiceModel.Dispatcher.IClientMessageInspector.AfterReceiveReply(System.ServiceModel.Channels.Message@,System.Object)" /> method.
- /// 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
- /// <paramref name="correlationState" /> objects are the same.
- /// </returns>
- public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)
- {
- SoapSecurityHeader header = new SoapSecurityHeader("UsernameToken-1", UserName, Password, "");
- request.Headers.Add(header);
- return header.Id;
- }
- /// <summary>
- /// Enables inspection or modification of a message after a reply message is received but prior to passing it back to the client application.
- /// </summary>
- /// <param name="reply">The message to be transformed into types and handed back to the client application.</param>
- /// <param name="correlationState">Correlation state data.</param>
- public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
- {
- var a = reply;
- // Nothing special here
- }
- }
下面是写入Head的部分
- 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();
- }
- }
至此大功告成!
扩展Wcf call security service, 手动添加 Soap Security Head.的更多相关文章
- WCF 在VS中,添加服务引用,地址输入http://ip/Service.svc,点击前往,提示错误,内容如下:
WCF的service端的webconfig如下: <?xml version="1.0"?> <configuration> <system.ser ...
- centos6.5 gsoap安装过程+ php添加soap扩展
参考博客: CentOS编译安装gSOAP Linux C实现webservice调用 安装gsoap流程 里面提到make时可能碰到的问题 还没有用到 1.从官网下载最新的版本:http://so ...
- wcf和web service的区别
1.WebService:严格来说是行业标准,不是技术,使用XML扩展标记语言来表示数据(这个是夸语言和平台的关键).微软的Web服务实现称为ASP.NET Web Service.它使用Soap简单 ...
- WCF和Web Service的 区(guan)别(xi)
参考文献:http://social.microsoft.com/Forums/zh-CN/c06420d1-69ba-4aa6-abe5-242e3213b68f/wcf-webservice 之前 ...
- WCF与 Web Service的区别是什么?各自的优点在哪里呢?
这是很多.NET开发人员容易搞错的问题.面试的时候也经常遇到,初学者也很难分快速弄明白 Web service: .net技术中其实就指ASP.NET Web Service,用的时间比较长,微软其实 ...
- 如何手动添加Windows服务和如何把一个服务删除
windows 手动添加服务方法一:修改注册表 在注册表编辑器,展开分支"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services" ...
- Windows服务的手动添加和删除方法
Windows服务的手动添加和删除方法 服务,是指执行指定系统功能的程序.例程或进程,以便支持其他程序,尤其是低层(接近硬件)程序.其实,服务就是一种特殊的应用程序,它从服务启动开始就一直处于运行状态 ...
- 解决 Cocos2d-x 中 Android.mk 手动添加源文件
转自:http://blog.csdn.net/ypfsoul/article/details/8909178 Makefile Android.mk 引发的思索 在我们编写 Android 平台 c ...
- Quartz动态添加定时任务执行sql(服务启动添加+手动添加)
系统用来每天插入视图数据... 一.数据库表设计 1.接口配置表(t_m_db_interface_config) 2.接口日志表(t_m_db_interface_log) 3.前端配置页面 查询页 ...
随机推荐
- Leetcode Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- 转:【译】CSS3:clip-path详解
我的一个学生,Heather Banks,想要实现他在Squarespace看到的一个效果: 根据她的以往经验,这个网站的HTML和CSS是完全在她的能力范围以内,于是我帮助她完成了这个效果.显示na ...
- ZeroMQ接口函数之 :zmq_inproc – ØMQ 本地进程内(线程间)传输方式
ZeroMQ API 目录 :http://www.cnblogs.com/fengbohello/p/4230135.html ——————————————————————————————————— ...
- Python获取当前日期及时间
import time def GetNowTime(): return time.strftime("%Y%m%d%H%M%S",time.localtime(time.time ...
- Bash On Win10 (WSL) 安装 Odoo 开发环境
前段时间微软发布了Bash On Win10,虽然目前还是Beta阶段,但是一想到再也不用折腾虚拟机上跑odoo了,就忍不住手痒,尝试在WSL上安装了一下odoo,结果比较惊喜,感觉可以抛弃Vitru ...
- POJ 1274 The Perfect Stall、HDU 2063 过山车(最大流做二分匹配)
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24081 Accepted: 106 ...
- Socket--Java
Server.java package socket; import java.io.BufferedReader; import java.io.DataInputStream; import ja ...
- requirejs代码结构分析
一.函数入口函数. req = requirejs = function (deps, callback, errback, optional) { //Find the right context, ...
- python logging usage
python中,logging模块主要是处理日志的. 所谓日志,可理解为在软件运行过程中,所记录的的一些运行情况信息 软件开发人员可以根据自己的需求添加日志,日志可以帮助软件开发人员 了解软件的运行信 ...
- 读懂UI设计的心理学
好文转载,版权归原作者 作为UI设计师,对待用户就像对待婴儿,知道如何通过界面设计诱导用户非常重要,这就需要了解心理学方面的知识了.今天分享一篇日本设计师的好文,结合心理学与设计,教你读懂心理学,提高 ...