您的位置:首页 > 理论基础 > 计算机网络

WCF BasicHttpBinding 安全解析(1)BasicHttpBinding基本配置

2011-06-27 12:56 471 查看
BasicHttpBinding使用HTTP作为传输协议用于发送SOAP1.1消息。服务可以使用此绑定来公开符合WS-IBP1.1标准的终结点,如ASMX客户端访问的终结点。同样,客户端可以使用BasicHttpBinding与公开符合WS-IBP1.1标准的终结点的服务(如ASMXWeb服务或采用BasicHttpBinding配置的服务)进行通信。

默认情况下,安全性处于禁用状态,但是通过在BasicHttpBinding(BasicHttpSecurityMode)构造函数中将BasicHttpSecurityMode设置为不同于None的值,可以添加安全性。默认情况下,它使用“Text”消息编码和UTF-8文本编码。

基于在11.2节我们使用的HelloService服务,我们这里使用BasicHttpBinding来对外发布它。

服务代码与之之前没有什么变化,如代码清单11-68。

代码清单11-68HelloService服务

publicclassHelloService:IHelloService

[code]
{


publicstringGetHello()


{


if(ServiceSecurityContext.Current!=null)


{


if(!ServiceSecurityContext.Current.IsAnonymous)


{


return"Hello:"+ServiceSecurityContext.Current.PrimaryIdentity.Name+";type="


+ServiceSecurityContext.Current.PrimaryIdentity.AuthenticationType;


}


return"";


}


else


{


return"hello";


}


}


}


[/code]

我们新建一个控制台项目,名为“basicHttpBindingHost”,用户做自定义宿主。宿主的配置如代码清单11-69。

代码清单11-69服务宿主配置


<?xmlversion="1.0"?>

[code]
<configuration>


<system.serviceModel>


<services>


<servicename="WcfSecurityExampleServiceLibrary.HelloService"behaviorConfiguration="mex">


<host>


<baseAddresses>


<addbaseAddress="http://127.0.0.1:64567/"/>


</baseAddresses>


</host>


<endpointaddress="http://127.0.0.1:64567/HelloService"binding="basicHttpBinding"


name="basicHttpHelloEndPoint"


contract="WcfSecurityExampleServiceLibrary.IHelloService"/>


<endpointaddress="mex"binding="mexHttpBinding"contract="IMetadataExchange"></endpoint>


</service>


</services>


<behaviors>


<serviceBehaviors>


<behaviorname="mex">


<serviceMetadata/>


</behavior>


</serviceBehaviors>


</behaviors>


</system.serviceModel>


<startup>


<supportedRuntimeversion="v4.0"sku=".NETFramework,Version=v4.0"/>


</startup>


</configuration>


[/code]

看代码清单11-69所示的配置,我们通过


<endpointaddress="http://127.0.0.1:64567/HelloService"binding="basicHttpBinding"

[code]
name="basicHttpHelloEndPoint"


contract="WcfSecurityExampleServiceLibrary.IHelloService"/>

[/code]

来设置当前终结点的绑定为basicHttpBinding,契约为WcfSecurityExampleServiceLibrary.IHelloService,并通过元数据终结点对外发布服务。宿主的代码如代码清单11-70所示。

代码清单11-70服务宿主的实现


classProgram

[code]
{


staticvoidMain(string[]args)


{


ServiceHosthostForHello=newServiceHost(typeof(HelloService));


hostForHello.Open();


try


{


while(true)


{


}


}


catch


{


hostForHello.Abort();


}


}


}


[/code]

启动宿主程序,我们在浏览器中输入http://127.0.0.1:64567/,结果如图11-27。





图11-27服务启动成功

从如11-27的结果我们可以看出服务启动成功并在指定端口监听。下面我们构造客户端,首先看客户端配置文件,如代码清单11-71。

代码清单11-71客户端配置


<?xmlversion="1.0"encoding="utf-8"?>

[code]
<configuration>


<system.serviceModel>


<client>


<endpointaddress="http://127.0.0.1:64590/HelloService"binding="basicHttpBinding"


contract="WcfSecurityExampleServiceLibrary.IHelloService"name="basicHttpHelloEndPoin"/>


</client>


</system.serviceModel>


</configuration>

[/code]

如代码清单11-71,客户端的配置很简单,只是指定终结点和契约,其他的配置采用默认配置。这里需要注意的是终结点指定的address=http://127.0.0.1:64590/HelloService,端口64590是我使用TcpTrace进行监听的端口,如图11-28。这里没有使用netTcpBinding在Behavior中设置的原因为BasicHttpBinding的AddressingVersion值为None,MessageVersion是不能改变的,只有CustomBinding才支持textMessageEncoding的设定。





11-28tctTrace监听转发消息

客户端调用代码如代码清单11-72。

代码清单11-72客户端调用代码


classProgram

[code]
{


staticvoidMain(string[]args)


{


using(ChannelFactory<IHelloService>channelFactory=newChannelFactory<IHelloService>("basicHttpHelloEndPoint"))


{


IHelloServicehelloService=channelFactory.CreateChannel();


using(helloServiceasIDisposable)


{


Console.WriteLine(helloService.GetHello());


}


}


Console.Read();


}


}

[/code]

调用代码和之前使用的并无差别,这里就不详说了。我们直接看运行结果,如图11-29。





图11-29客户端运行结果

我们的服务返回的信息应该包含客户端用户名和验证类型,但是图11-29中没有这两项信息,原因在于默认情况下BasicHttpBinding不采用任何安全配置。我们再来看tcpTrace的监听结果,如图11-30。





图11-30tcpTrace监听信息

从图11-30的监听结果来看,采用的是明文传输,编码格式为“text/xml”,没有任何认证和凭据信息。

一个完整的BasicHttpBinding的示例到这里演示完毕了,下面我们对它做进一步的分析。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: