原创地址:http://www.cnblogs.com/jfzhu/p/4071342.html

转载请注明出处

前面文章介绍了《WCF basicHttpBinding之Message Security Mode》如何basicHttpBinding的Message Security Mode,并且clientCredentialType用的是certificate。

本文演示basicHttpbinding使用Transport Security Mode,并且clientCredentialType="None"。

(一)WCF 服务代码与配置文件

IDemoService.cs

using System.ServiceModel;

namespace WCFDemo
{
[ServiceContract(Name = "IDemoService")]
public interface IDemoService
{
[OperationContract]
[FaultContract(typeof(DivideByZeroFault))]
int Divide(int numerator, int denominator);
}
}

DemoService.cs

using System;
using System.ServiceModel;
using System.ServiceModel.Activation; namespace WCFDemo
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class DemoService : IDemoService
{
public int Divide(int numerator, int denominator)
{
try
{
return numerator / denominator;
}
catch (DivideByZeroException ex)
{
DivideByZeroFault fault = new DivideByZeroFault();
fault.Error = ex.Message;
fault.Detail = "Denominator cannot be ZERO!";
throw new FaultException<DivideByZeroFault>(fault);
}
}
}
}

完整的代码也可以参见《WCF服务创建与抛出强类型SOAP Fault》

server web.config

<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="basicBinding">
<security mode="Transport">
<transport clientCredentialType="None" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="WCFDemo.DemoService" behaviorConfiguration="CustomBehavior">
<endpoint address="DemoService" binding="basicHttpBinding" contract="WCFDemo.IDemoService" bindingConfiguration="basicBinding" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="CustomBehavior">
<serviceMetadata httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>

(二)为WCF Service application添加一个https binding。

具体作法参见《Step by Step 配置使用HTTPS的ASP.NET Web应用》

配置完https binding之后,双击SSL Settings

勾选Require SSL,点击Apply。

Http的Binding还是不可缺少,否则会出现下面的错误

(三)在客户端安装SSL根证书

由于https证书使用的是

所以我们使用的WCF Service URL为 https://win-ounm08eqe64.henry.huang/DemoService.svc

在客户端,为C:\Windows\System32\Drivers\etc\host 添加一条记录

然后安装根证书

双击根证书文件,弹出证书属性的对话框,此时该根证书并不受信任,我们需要将其加入“受信任的根证书颁发机构”,点击安装证书

(四)客户端代码与配置文件

在客户端Visual Studio添加Service Reference

private void buttonCalculate_Click(object sender, EventArgs e)
{
try
{
textBoxResult.Text = demoServiceClient.Divide(Convert.ToInt32(textBoxNumerator.Text), Convert.ToInt32(textBoxDenominator.Text)).ToString();
}
catch (FaultException<DemoServiceReference.DivideByZeroFault> fault)
{
MessageBox.Show(fault.Detail.Error + " - " + fault.Detail.Detail);
}
}

client app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IDemoService">
<security mode="Transport" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://win-ounm08eqe64.henry.huang/DemoService.svc/DemoService"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IDemoService"
contract="DemoServiceReference.IDemoService" name="BasicHttpBinding_IDemoService" />
</client>
</system.serviceModel>
</configuration>

(五)运行代码,监听Message

使用Fiddler,发现消息全部加密

但是如果用Microsoft Service Trace Viewer查看Message Log(参见《使用WCF的Trace与Message Log功能 》),可以看到解密后的信息,因为它不是在wire上监听,而Fiddler是在wire上进行监听。

Request:

Response:

(六)总结

Transport Security Mode是传输协议级的加密,而Message Security Mode是对消息级别的加密。每种协议都有自己对应的传输协议级的加密方式,比如HTTP的加密方式就为SSL。

WCF basicHttpBinding之Transport Security Mode, clientCredentialType="None"的更多相关文章

  1. WCF wsHttpBinding之Transport security Mode, clientCredentialType=”Basic”

    原创地址:http://www.cnblogs.com/jfzhu/p/4071342.html 转载请注明出处 如何在WCF中使用Transport Security Mode,以及如何创建证书,请 ...

  2. WCF basicHttpBinding之Message Security Mode

    原创地址:http://www.cnblogs.com/jfzhu/p/4067873.html 转载请注明出处 前面的文章<WCF Security基本概念>介绍了WCF的securit ...

  3. iOS App 不支持http协议 App Transport Security has blocked a cleartext HTTP (http://)

    目前iOS已经不支持http协议了,不过可以通过info.plist设置允许 App Transport Security has blocked a cleartext HTTP (http://) ...

  4. App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file

    ios进行http请求,会出现这个问题: App Transport Security has blocked a cleartext HTTP (http://) resource load sin ...

  5. App Transport Security has blocked a cleartext HTTP (http://)

    使用SDWebImage加载“http://”开头的图片报错,错误如下: App Transport Security has blocked a cleartext HTTP (http://) r ...

  6. iOS9中的App Transport Security

    问题:webView加载网页加载不出来 原因:苹果在iOS9 sdk中加入了App Transport Security限制(iOS9以前的iOS sdk默认关闭ATS),默认强制使用https,并且 ...

  7. 网络请求报错:The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.

    iOS9引入了新特性App Transport Security (ATS).详情:App Transport Security (ATS) 如果你想设置不阻止任何网络,只需要在info.plist文 ...

  8. IOS开发 App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.

    xcode自7后不再使用http,而是使用https请求,但目前很多网络请求还只是以http请求,我们可以这样解决 info.plist->添加@“App Transport Security ...

  9. App Transport Security has blocked a cleartext

    错误描述: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecu ...

随机推荐

  1. Quartz定时调度框架

    Quartz定时调度框架CronTrigger时间配置格式说明 CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年 ...

  2. 关于linux服务器上搭建ftp服务的流程

    小龙最近折腾了一个阿里云的服务器,买完了就要开始做那么多那么多的功课,小龙对ssh也是一知半解的状态,做个小笔记,发布下整个ftp服务的搭建过程,大神勿喷:) 一.aliyun Linux(Redha ...

  3. D3中selection之使用

    1. 极为重要的reference: [1] How selections works. http://bost.ocks.org/mike/selection/ [2] Nested selecti ...

  4. FFmpeg纯净版解码 av_parser_parse2

    主要是通过av_parser_parse2拿到AVPaket数据,跟av_read_frame类似. 输入必须是只包含视频编码数据“裸流”(例如H.264.HEVC码流文件),而不能是包含封装格式的媒 ...

  5. unity3d关于碰撞问题

    这个是我做忍者游戏出现的问题,做个记录也为以后有人遇到也可以借鉴.因为刚接触unity,所以对其所知甚少,说错的地方请指教. 问题:角色碰撞墙为什么会先触发碰撞地面,然后再触发碰撞墙 想要的效果:是角 ...

  6. python 面向对象初级篇

    Python 面向对象(初级篇) 概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发" ...

  7. log4j:WARN Please initialize the log4j system properly 问题解决

    log4j:WARN No appenders could be found for logger (com.netease.qa.testng.TestngRetry).log4j:WARN Ple ...

  8. java 日期格式

  9. js转盘抽奖

    这个是很简易的转盘,只用了html,css,js 通过css产生一个转盘上的指针,用js动态改变css中的transparent改变指针的角度.再添加一个背景图片类似于奖项的转盘 <!DOCTY ...

  10. 4.3 多线程进阶篇<中>(GCD)

    更正:队列名称的作用的图中,箭头标注的有些问题,已修正 本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书” 本文源码 Demo 详见 Gith ...