【WCF安全】SOAP消息实现用户名验证:通过OperationContext直接添加/访问MessageHeader信息
服务代码
1.契约
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text; namespace 消息拦截实现用户名验证
{
[ServiceContract(Namespace="http://localhost/")]
public interface IService1
{
[OperationContract]
string GetData(int value); }
}
2.服务实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text; namespace 消息拦截实现用户名验证
{
public class Service1 : IService1
{
public string GetData(int value)
{
string msg = OperationContext.Current.RequestContext.RequestMessage.ToString();
// 注意namespace必须和ServiceContract中定义的namespace保持一致,默认是:http://tempuri.org
var ns = "http://localhost/";
var user = GetHeaderValue("user", ns);
var pwd = GetHeaderValue("pwd", ns);
// 验证失败
if (user != "Guest01" || pwd != "")
return msg += "/r/n用户名密码错误";
return msg += "/r/n" + string.Format("You entered: {0}", value);
}
private string GetHeaderValue(string name, string ns = "http://tempuri.org")
{
var headers = OperationContext.Current.IncomingMessageHeaders;
var index = headers.FindHeader(name, ns);
if (index > -)
return headers.GetHeader<string>(index);
else
return null;
}
}
}
3.服务配置(Demo中采用的是默认配置)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
<serviceMetadata httpGetEnabled="true"/>
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
客户端
1.客户端代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Channels;
using System.ServiceModel; namespace 客户端
{
class Program
{
static void Main(string[] args)
{
try
{
SR01.Service1Client sc = new SR01.Service1Client();
using (var scope = new OperationContextScope(sc.InnerChannel))
{
// 注意namespace必须和ServiceContract中定义的namespace保持一致,默认是:http://tempuri.org
var myNamespace = "http://localhost/";
// 注意Header的名字中不能出现空格,因为要作为Xml节点名。
var user = MessageHeader.CreateHeader("user", myNamespace, "Guest01");
var pwd = MessageHeader.CreateHeader("pwd", myNamespace, "");
OperationContext.Current.OutgoingMessageHeaders.Add(user);
OperationContext.Current.OutgoingMessageHeaders.Add(pwd);
string res = sc.GetData();
Console.WriteLine(res);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
2.客户端配置
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="" maxBufferPoolSize="" maxReceivedMessageSize=""
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="" maxStringContentLength="" maxArrayLength=""
maxBytesPerRead="" maxNameTableCharCount="" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:7105/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="SR01.IService1"
name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
</configuration>
返回结果:
红框部分为正常返回值;其它部分为SOAP XML内容
【WCF安全】SOAP消息实现用户名验证:通过OperationContext直接添加/访问MessageHeader信息的更多相关文章
- WCF 学习总结5 -- 消息拦截实现用户名验证(转)
WCF建立在基于消息的通信这一概念基础上.通过方法调用(Method Call)形式体现的服务访问需要转化成具体的消息,并通过相应的编码(Encoding)才能通过传输通道发送到服务端:服务操作执行的 ...
- webservice系统学习笔记5-手动构建/发送/解析SOAP消息
手动拼接SOAP消息调用webservice SOAP消息的组成: 1.创建需要发送的SOAP消息的XML(add方法为例子) /** * 创建访问add方法的SOAP消息的xml */ @Test ...
- 如何在WCF中用TcpTrace工具查看发送和接收的SOAP消息
WCF对消息加密(只对消息加密,不考虑Authorize)其实很简单,只要在server和client端的binding加入security mode为Message(还有Transport, Tra ...
- WCF安全:通过 扩展实现用户名密码认证
在webSservice时代,可以通过SOAPHEADER的方式很容易将用户名.密码附加到SOAP header消息头上,用户客户端对调用客户端身份的验证.在WCF 时代,也可以通过Operation ...
- 【.net 深呼吸】记录WCF的通信消息
前面老周给大伙伴们介绍了把跟踪信息写入日志文件的方法,今天咱们换个类似的话题来扯一下,对了,咱们就说说怎么把WCF的往来消息log下来吧. 尽管在现实生活中,我们不主张偷窥他人信息,不过,偷窥程序信息 ...
- webservice05#soap消息
1, SOAPMessage结构图 2, SOAP消息的创建 1>前面的一个简单WebService 服务 package com.yangw.soap.service; import jav ...
- WCF基础之消息协定
通常定义消息的架构,使用数据协定就够了,但是有时必须将类型精确映射到soap消息,方法两种:1.插入自定义soap标头:2.另一种是定义消息的头和正文的安全属性.消息协定通过MessageContra ...
- wcf中的消息模式
1请求响应模式 a.wcf中的消息模式默认是请求响应模式 b.返回值是void默认也是请求响应模式,可返回服务端的错误信息 c.客户端在请求后,当前线程停止真到接受收服务器的响应 [Opereatio ...
- ajax基础语法、ajax做登录、ajax做用户名验证是否可用、ajax做关键字查询动态显示、ajax做用表格显示数据并增加操作列
AJAX: AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新. ...
随机推荐
- CVE补丁安全漏洞【学习笔记】
更新安卓系统的CVE补丁网站:https://www.cvedetails.com/vulnerability-list/vendor_id-1224/product_id-19997/version ...
- 并发-Synchronized底层优化(偏向锁、轻量级锁)
Synchronized底层优化(偏向锁.轻量级锁) 参考: http://www.cnblogs.com/paddix/p/5405678.html 一.重量级锁 上篇文章中向大家介绍了Synchr ...
- JavaScript中this关键字的使用比较
JavaScript中this关键字的使用比较 this关键字在JavaScript中,用的不能说比较多,而是非常多.那么熟悉this关键字的各种用法则显得非常关键. this有时候就是我们经常说的上 ...
- 关于eclipse中看不到源码的问题
这几步之后会生成一个src压缩包,再用att打开即可.记住 要按ctrl再点击
- Hibernate与JDBC、EJB、JDO的比较
常用的数据库操作包括:JDBC.EJB.JDO以及Hibernate.它的各有优缺点: (1) JDBC:多数Java开发人员是用JDBC来和数据库进行通信,它可以通过DAO模式进行改善和提高.但这种 ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- 第11章:最长公共子序列(LCS:Longest Common Subsequence)
方法:动态规划 <算法导论>P208 最优子结构 + 重叠子问题 设xi,yi,为前i个数(前缀) 设c[i,j]为xi,yi的LCS的长度 c[i,j] = 0 (i ==0 || j ...
- python学习笔记(virtualenv下载安装)
之前博客评论中有人建议我面对多个python版本的情况.可以使用virtualenv这个python虚拟沙盒 首页是利用pip下载.关于pip如何下载安装前面的博客中已经提到就不细说 cmd直接进入p ...
- Shell for、while循环
先顺带说下 if 1. if 条件;then else fi 如果else分支没有执行语句,可以不写. 2. if 条件;then elif 条件;then else fi #!/bin/bash ...
- 教你10步闯进google play排行榜前列
1.正视最高榜单的价值 我们需要了解排名对你的游戏的价值,进入前20名你的游戏获得每日至少1万5千的安装量,而前10名获得至少2万5千的安装量.通过奖励性广告网络而获得这些流量需要你每日支付至 ...