网络负载均衡环境下wsHttpBinding+Message Security+Windows Authentication的常见异常

来源:互联网 发布:plc步进电机编程实例 编辑:程序博客网 时间:2024/04/25 23:10

提高Windows Communication Foundation (WCF) 应用程序负载能力的方法之一就是通过把它们部署到负载均衡的服务器场中. 其中可以使用标准的负载均衡技术, Windows 网络负载均衡(NLB)的软件(例如Application Request Routing), 或者硬件(F5)实现NLB的功能. 随着这些NLB场景变得越来越复杂, 对WCF的架构带来了越来越多的挑战. 本文仅对wsHttpBinding+Message Security+Windows Authentication在NLB的环境下最常见的文件进行讨论..

%26nbsp;

介绍 :

%26nbsp;

通过阅读本文, 您可以了解到:

  1. NLB下wsHttpBinding+Message Security+Windows Authentication常见的异常.`
  2. WCF Message Security的Windows Authentication是如何进行的.
  3. 提供几种典型的WCF Security的配置方式.

%26nbsp;

预备知识

%26nbsp;

WCF wsHttpBinding默认采用Message Security模式. 相比Transport Security模式,它有很多优点。例如:提供了End-to-End security, 允许选择性的加密部分消息,与传输协议无关可以用于任何传输协议,提供更多的安全选择。更多内容可以参考 :

http://msdn.microsoft.com/en-us/library/ff648863.aspx

http://msdn.microsoft.com/en-us/library/ms733137(v=vs.110).aspx

%26nbsp;

Message Security 下有多种客户端验证方式。 其中Windows authentication是比较常用的Credential Type,并且是默认值。更多内容可以参考:

http://msdn.microsoft.com/en-us/library/ms731346(v=vs.110).aspx

%26nbsp;

%26nbsp;

常见问题 :

%26nbsp;

在Standalone的服务器上WCF Security能够很好的工作. 但是在NLB的环境上, 由于错误的配置方式, 可能会遇到下面的常见错误 :

%26nbsp;

l%26nbsp; The SecurityContextSecurityToken with context-id=urn:uuid:xxxx(key generation-id=) is not registered. at System.ServiceModel.Security.WSSecureConversation.SecurityContextTokenEntry.ReadTokenCore

l%26nbsp; %26lt;System.ServiceModel.Security.SecurityNegotiationException: Cannot find the negotiation state for the context 'uuid-xxxx

%26nbsp;

%26nbsp;

WsHttpBinding+Message Security+Windows Authentication是如何工作的 :

%26nbsp;

要了解这两个错误是如何发生的, 首先需要了解正常情况下wsHttpBinding Message Security是如何进行Windows 验证的.

%26nbsp;

一般情况下, wsHttpBinding按照SPNEGO来引导一个SecurityContextToken(SCT)的生成, 并且将SCT缓存到服务器端. STCs 是基于对称密钥生成的,能够非常有效的签注/加密(signing/encrypting)消息。 当确定客户端会向服务器端发送多次请求的时候,这是一种非常好的提供效率的方式。通过观察WCF TRACE,一般来说会看到这样的过程。首先客户端和服务端生成一个SCT,然后进行一个SPNEGO的过程(有可能是多次请求才能完成),最后客户端和服务端交换SCT并且使用SCT加密WCF的调用。

下面比较详细的列举出整个步骤

  1. 客户端首先进行SP-Nego 握手, 这个过程完成后会生成一个SCT。

1)%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp; 客户端先发送一个RequestSecurity Token(RST)。RST的目的是生成SP-Nego TokenType,它包含了一个SP-Nego块.

2)%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp; 服务端返回一个Request Security Token Response (RSTR).

3)%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp; 如果需要的话,客户端会将RSTR再次发送给服务端.

4)%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp; 服务端将一个对称密钥包裹在STC里, 又将STC包裹在RequestSecurity Token Response Collection (RSTRC). 然后把RSTRC发送给客户端.

一般来说3)和4)可能会发生多次, 一般情况下可能会有4次, 2次客户端2次服务端.

%26nbsp;

  1. 客户端向服务端请求另外一个SCT用于消息层面的security。这个SCT是一个长生命是周期,并且真正代表了客户端和服务端之间的session key.

1)%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp; 客户端用SCT加注并且加密RST, 并把RST发送到服务端.

2)%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp; 服务器端用SCT加注并加密一个RSTR并发送会客户端. 这个RSTR中包含了一个SCT.

这里仅发生一次RST/RSTR.

%26nbsp;

  1. 客户端在得到 2 所产生的SCT之后,开始执行WCF调用。

1)%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp; 客户端使用上一步所得到的SCT来加密消息. 进行WCF调用.

2)%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp; 服务端执行完成之后, 使用同一的SCT来加密消息并返回给客户的. 在这里addressing headers会被签注, 消息体会被签注并且加密.

%26nbsp;

问题分析 :

%26nbsp;

问题一 :

%26nbsp;

The SecurityContextSecurityToken with context-id=urn:uuid:a02a1879-3297-4dee-8035-68eb30ed4195 (key generation-id=) is not registered.

at System.ServiceModel.Security.WSSecureConversation.SecurityContextTokenEntry.ReadTokenCore(XmlDictionaryReader reader, SecurityTokenResolver tokenResolver)

at System.ServiceModel.Security.WSSecurityTokenSerializer.ReadTokenCore(XmlReader reader, SecurityTokenResolver tokenResolver)

at System.IdentityModel.Selectors.SecurityTokenSerializer.ReadToken(XmlReader reader, SecurityTokenResolver tokenResolver)

at System.ServiceModel.Security.ReceiveSecurityHeader.ReadToken(XmlReader reader, SecurityTokenResolver tokenResolver, IList1 allowedTokenAuthenticators, SecurityTokenAuthenticator%26amp;amp;amp; usedTokenAuthenticator)

at System.ServiceModel.Security.ReceiveSecurityHeader.ReadToken(XmlDictionaryReader reader, Int32 position, Byte[] decryptedBuffer, SecurityToken encryptionToken, String idInEncryptedForm, TimeSpan timeout)

at System.ServiceModel.Security.ReceiveSecurityHeader.ExecuteFullPass(XmlDictionaryReader reader)

at System.ServiceModel.Security.StrictModeSecurityHeaderElementInferenceEngine.ExecuteProcessingPasses(ReceiveSecurityHeader securityHeader, XmlDictionaryReader reader)

at System.ServiceModel.Security.ReceiveSecurityHeader.Process(TimeSpan timeout)

at System.ServiceModel.Security.MessageSecurityProtocol.ProcessSecurityHeader(ReceiveSecurityHeader securityHeader, Message%26amp;amp;amp; message, SecurityToken requiredSigningToken, TimeSpan timeout, SecurityProtocolCorrelationState[] correlationStates)

at System.ServiceModel.Security.SymmetricSecurityProtocol.VerifyIncomingMessageCore(Message%26amp;amp;amp; message, String actor, TimeSpan timeout, SecurityProtocolCorrelationState[] correlationStates)

at System.ServiceModel.Security.MessageSecurityProtocol.VerifyIncomingMessage(Message%26amp;amp;amp; message, TimeSpan timeout, SecurityProtocolCorrelationState[] correlationStates)

at System.ServiceModel.Channels.SecurityChannelListener1.ServerSecurityChannel1.VerifyIncomingMessage(Message%26amp;amp;amp; message, TimeSpan timeout, SecurityProtocolCorrelationState[] correlationState)

at System.ServiceModel.Channels.SecurityChannelListener1.SecurityReplyChannel.ProcessReceivedRequest(RequestContext requestContext, TimeSpan timeout)

at System.ServiceModel.Channels.SecurityChannelListener1.ReceiveRequestAndVerifySecurityAsyncResult.ProcessInnerItem(RequestContext innerItem, TimeSpan timeout)

at System.ServiceModel.Channels.SecurityChannelListener1.ReceiveItemAndVerifySecurityAsyncResult2.OnInnerReceiveDone()

at System.ServiceModel.Channels.SecurityChannelListener1.ReceiveItemAndVerifySecurityAsyncResult2.InnerTryReceiveCompletedCallback(IAsyncResult result)

at System.ServiceModel.Diagnostics.Utility.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)

at System.ServiceModel.AsyncResult.Complete(Boolean completedSynchronously)

at System.ServiceModel.Channels.InputQueue1.AsyncQueueReader.Set(Item item)

at System.ServiceModel.Channels.InputQueue1.Dispatch()

at System.ServiceModel.Channels.InputQueue1.OnDispatchCallback(Object state)

at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.WorkItem.Invoke2()

at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.WorkItem.OnSecurityContextCallback(Object o)

at System.Security.SecurityContext.Run(SecurityContext securityContext, ContextCallback callback, Object state)

at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.WorkItem.Invoke()

at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.ProcessCallbacks()

at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.CompletionCallback(Object state)

at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)

at System.ServiceModel.Diagnostics.Utility.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)

at System.Threading.IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped pOVERLAP)

%26nbsp;

这个错误在服务端和客户端均能看到, 但大多数情况下是在服务端发生, 并且将这一的错误饭或到了客户端. 这个错误的字面意思就是找不到SCT. 但是既然能够肯到STC的UUID, 那么说明这个SCT确实存在.

参照上面STC生成的过程可以看到, STC是服务端和客户端进过多次的交涉最终才能形成. 这个STC只能在这个服务端和客户端之间传递, 并不能分享给第三方. 这是出于安全的考虑.

这个问题最常见于NLB的环境中.如果NLB设备或者软件没有启用Sticky Session. 在有多台WCF服务器的情况下, 客户端发起的请求很有可能被转移到另外一台服务器上. 由于STC并不会被第三方获取, 那么另外一个服务器上不会有相同的SCT. 因此服务端会认为这个STC并没有被注册.

%26nbsp;

%26nbsp;

问题二:

%26nbsp;

%26lt;ExceptionType%26gt;System.ServiceModel.Security.SecurityNegotiationException, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089%26lt;/ExceptionType%26gt;

%26lt;Message%26gt;Cannot find the negotiation state for the context 'uuid-954e94c0-6e42-4816-b53f-bc75e18a9534-2'.%26lt;/Message%26gt;

%26lt;StackTrace%26gt;

at System.ServiceModel.Security.NegotiationTokenAuthenticator`1.ProcessRequestCore(Message request)

at System.ServiceModel.Security.NegotiationTokenAuthenticator`1.NegotiationHost.NegotiationSyncInvoker.Invoke(Object instance, Object[] inputs, Object[]%26amp;amp; outputs)

at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc%26amp;amp; rpc)

at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc%26amp;amp; rpc)

at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc%26amp;amp; rpc)

at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc%26amp;amp; rpc)

at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc%26amp;amp; rpc)

at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc%26amp;amp; rpc)

at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

at System.ServiceModel.Dispatcher.ChannelHandler.DispatchAndReleasePump(RequestContext request, Boolean cleanThread, OperationContext currentOperationContext)

at System.ServiceModel.Dispatcher.ChannelHandler.HandleRequest(RequestContext request, OperationContext currentOperationContext)

at System.ServiceModel.Dispatcher.ChannelHandler.AsyncMessagePump(IAsyncResult result)

at System.ServiceModel.Dispatcher.ChannelHandler.OnAsyncReceiveComplete(IAsyncResult result)

at System.ServiceModel.Diagnostics.Utility.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)

at System.ServiceModel.AsyncResult.Complete(Boolean completedSynchronously)

at System.ServiceModel.Channels.InputQueue`1.AsyncQueueReader.Set(Item item)

at System.ServiceModel.Channels.InputQueue`1.Dispatch()

at System.ServiceModel.Channels.InputQueue`1.OnDispatchCallback(Object state)

at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.WorkItem.Invoke2()

at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.WorkItem.OnSecurityContextCallback(Object o)

at System.Security.SecurityContext.Run(SecurityContext securityContext, ContextCallback callback, Object state)

at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.WorkItem.Invoke()

at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.ProcessCallbacks()

at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.CompletionCallback(Object state)

at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)

at System.ServiceModel.Diagnostics.Utility.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)

at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)

%26lt;/StackTrace%26gt;

%26nbsp;

这个问题较多情况是发生在SPNego的阶段. 从上面介绍的步骤能够看到, SPNego的整个阶段很长,需要服务端和客户端进行多次的交流. 这这个交流的过程中, 任何一次请求被NLB递交到一个错误的服务器上都有可能造成这样的错误.

%26nbsp;

%26nbsp;

解决方案 :

%26nbsp;

造成上面问题的主要原因之一就是NLB的设备或者软件将客户端的请求递交到了一台错误的的服务器上. 要解决这一的问题, 最好的方法就是启用NLB上的Sticky Session. 在某些情形下, 如果NLB并不支持这种做法, 那么可以通过修改WCF配置来绕过这写问题.

%26nbsp;

为了解决第一个问题, 需要禁用Establishing Security Context. 禁用后, STC并不会被缓存起来, 也不会使用对称密钥, 而是改用非对称密钥, 而且每次都需要重新生成STC. 这种做法的副作用是降低WCF的性能.

%26nbsp;

 1 %26lt;bindings%26gt; 2  3    %26lt;wsHttpBinding%26gt; 4  5     %26lt;binding name=%26rdquo;MyServiceBinding%26rdquo;%26gt;     6  7      %26lt;security mode=%26rdquo;Message%26rdquo; %26gt; 8  9       %26lt;message establishSecurityContext=%26rdquo;false%26rdquo; clientCredentialType=%26rdquo;Windows%26rdquo; /%26gt;           10 11      %26lt;/security%26gt;12 13     %26lt;/binding%26gt;14 15    %26lt;/wsHttpBinding%26gt;16 17   %26lt;/bindings%26gt;

%26nbsp;

%26nbsp;

%26nbsp;

在解决第一个问题后, 仍然可能会遇到第二个问题. 因为默认情况下会服务端和客户端仍然会Negotiate Service Credential. 在这个过程中, 同样有可能会被NLB的行为中断从而引发问题. 可以通过禁用NegotiateServiceCredential来绕过这个问题.

 1 %26lt;bindings%26gt; 2  3    %26lt;wsHttpBinding%26gt; 4  5     %26lt;binding name=%26rdquo;MyServiceBinding%26rdquo;%26gt;     6  7      %26lt;security mode=%26rdquo;Message%26rdquo; %26gt; 8       %26lt;message establishSecurityContext=%26rdquo;false%26rdquo; NegotiateServiceCredential=%26rdquo;false%26rdquo; 9 10 clientCredentialType=%26rdquo;Windows%26rdquo; /%26gt;           11 12      %26lt;/security%26gt;13 14     %26lt;/binding%26gt;15 16    %26lt;/wsHttpBinding%26gt;17 18   %26lt;/bindings%26gt;

%26nbsp;

%26nbsp;

将属性设置为%26nbsp;true%26nbsp;需要客户端和服务支持 WS-Trust 和 WS-SecureConversation。%26nbsp;将属性设置为%26nbsp;false%26nbsp;时不需要 WS-Trust 或 WS-SecureConversation 受支持。对于 Windows 凭据,将此属性设置为%26nbsp;false%26nbsp;将导致基于%26nbsp;KerberosToken%26nbsp;的身份验证。%26nbsp;这要求客户端和服务都是 Kerberos 域的一部分。%26nbsp;此模式可与实现 OASIS Kerberos 令牌配置文件的 SOAP 堆栈交互操作。%26nbsp;将此属性为%26nbsp;true%26nbsp;会引起通过 SOAP 消息进行 SPNego 交换的 SOAP 协商. 如果将此属性设置为%26nbsp;false,并且将绑定配置为使用 Windows 作为客户端凭据类型,则必须将服务帐户与服务主体名称 (SPN) 相关联。%26nbsp;为此,请在 NETWORK SERVICE 帐户或 LOCAL SYSTEM 帐户下运行服务。%26nbsp;也可以使用 SetSpn.exe 工具为服务帐户创建一个 SPN。%26nbsp;不论何种情况,客户端都必须使用%26lt;servicePrincipalName%26gt;%26nbsp;元素中的正确 SPN,或者通过使用%26nbsp;EndpointAddress%26nbsp;构造函数来应用正确的 SPN。

%26nbsp;

除此之外, 还有下面的集中组合可以适用于NLB的环境中. 其中, BasicHttpBinding和wsHttpBinding均可以相互替换

%26bull;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp; BasicHttpBinding - TransportCredentialOnly - Windows

%26bull;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp; BasicHttpBinding - TransportCredentialOnly - Ntlm

%26bull;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp; BasicHttpBinding - TransportWithMessageCredential - UserName

%26bull;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp; BasicHttpBinding - TransportWithMessageCredential - Certificate

%26bull;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp;%26nbsp; wsHttpBinding - Transport - Ntlm

%26nbsp;

l%26nbsp;%26nbsp; BasicHttp %26ndash; TransportCredentialOnly %26ndash; Windows

Service %26ndash; web.config

%26nbsp;

 1 %26lt;basicHttpBinding%26gt; 2  3         %26lt;binding name="basicN"%26gt; 4  5           %26lt;security mode="TransportCredentialOnly"%26gt; 6  7             %26lt;transport clientCredentialType="Windows"/%26gt; 8  9           %26lt;/security%26gt;10 11         %26lt;/binding%26gt;12 13 %26lt;/basicHttpBinding%26gt;14 15  16 17 %26lt;endpoint address=""18 19                   binding="basicHttpBinding"20 21                   contract="WindowsAuthTestService.IService1"22 23                   bindingConfiguration="basicN"24 25                   /%26gt;

%26nbsp;

%26nbsp;

Client %26ndash; app.config

%26nbsp;

 1 %26lt;bindings%26gt; 2  3             %26lt;basicHttpBinding%26gt; 4  5                 %26lt;binding name="BasicHttpBinding_IService1"%26gt; 6  7                     %26lt;security mode="TransportCredentialOnly"%26gt; 8  9                         %26lt;transport clientCredentialType="Windows" /%26gt;10 11                     %26lt;/security%26gt;12 13                 %26lt;/binding%26gt;14 15             %26lt;/basicHttpBinding%26gt;16 17         %26lt;/bindings%26gt;18 19         %26lt;client%26gt;20 21             %26lt;endpoint address="http://localhost/WindowsAuthTestService/Service1.svc"22 23                 binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"24 25                 contract="ServiceReference1.IService1" name="BasicHttpBinding_IService1" /%26gt;26 27         %26lt;/client%26gt;

%26nbsp;

%26nbsp;

%26nbsp;

l%26nbsp;%26nbsp; BasicHttp %26ndash; TransportCredentialOnly %26ndash; Ntlm

Service %26ndash; web.config

%26nbsp;

 1 %26lt;basicHttpBinding%26gt; 2  3         %26lt;binding name="basicN"%26gt; 4  5           %26lt;security mode="TransportCredentialOnly"%26gt; 6  7             %26lt;transport clientCredentialType="Ntlm"/%26gt; 8  9           %26lt;/security%26gt;10 11         %26lt;/binding%26gt;12 13 %26lt;/basicHttpBinding%26gt;14 15  16 17 %26lt;endpoint address=""18 19                   binding="basicHttpBinding"20 21                   contract="WindowsAuthTestService.IService1"22 23                   bindingConfiguration="basicN"24 25                   /%26gt;

%26nbsp;

%26nbsp;

Client %26ndash; app.config

%26nbsp;

 1 %26lt;bindings%26gt; 2  3             %26lt;basicHttpBinding%26gt; 4  5                 %26lt;binding name="BasicHttpBinding_IService1"%26gt; 6  7                     %26lt;security mode="TransportCredentialOnly"%26gt; 8  9                         %26lt;transport clientCredentialType="Ntlm" /%26gt;10 11                     %26lt;/security%26gt;12 13                 %26lt;/binding%26gt;14 15             %26lt;/basicHttpBinding%26gt;16 17         %26lt;/bindings%26gt;18 19         %26lt;client%26gt;20 21             %26lt;endpoint address="http://pupanda-win8.fareast.corp.microsoft.com/WindowsAuthTestService/Service1.svc"22 23                 binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"24 25                 contract="ServiceReference1.IService1" name="BasicHttpBinding_IService1" /%26gt;26 27         %26lt;/client%26gt;

%26nbsp;

%26nbsp;

l%26nbsp;%26nbsp; BasicHttp %26ndash; TransportWithMessageCredential %26ndash; UserName

Service %26ndash; web.config

%26nbsp;

 1 %26lt;bindings%26gt; 2  3       %26lt;basicHttpBinding%26gt; 4  5         %26lt;binding name="b0"%26gt; 6  7           %26lt;security mode="TransportWithMessageCredential"%26gt; 8  9             %26lt;message clientCredentialType="UserName"/%26gt;10 11           %26lt;/security%26gt;12 13         %26lt;/binding%26gt;14 15       %26lt;/basicHttpBinding%26gt;16 17     %26lt;/bindings%26gt;18 19  20 21 %26lt;serviceBehaviors%26gt;22 23         %26lt;behavior name="sb"%26gt;24 25           %26lt;serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/%26gt;26 27           %26lt;serviceDebug includeExceptionDetailInFaults="true"/%26gt;28 29           %26lt;serviceCredentials%26gt;30 31             %26lt;userNameAuthentication userNamePasswordValidationMode="Windows" /%26gt;32 33             %26lt;serviceCertificate findValue="11 12 32 12"34 35                   storeLocation="LocalMachine"36 37                   storeName="My"38 39                   x509FindType="FindBySerialNumber"/%26gt;40 41           %26lt;/serviceCredentials%26gt;42 43         %26lt;/behavior%26gt;44 45       %26lt;/serviceBehaviors%26gt;

%26nbsp;

%26nbsp;

Client %26ndash; app.config

%26nbsp;

 1 %26lt;bindings%26gt; 2  3             %26lt;basicHttpBinding%26gt; 4  5                 %26lt;binding name="BasicHttpBinding_IService1"%26gt; 6  7                     %26lt;security mode="TransportWithMessageCredential" /%26gt; 8  9                 %26lt;/binding%26gt;10 11             %26lt;/basicHttpBinding%26gt;12 13         %26lt;/bindings%26gt;14 15         %26lt;client%26gt;16 17             %26lt;endpoint address="https://server/TransportMessageService/Service1.svc"18 19                 binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"20 21                 contract="ServiceReference1.IService1" name="BasicHttpBinding_IService1" /%26gt;22 23         %26lt;/client%26gt;

%26nbsp;

BasicHttp %26ndash; TransportWithMessageCredential %26ndash; Certificate

Service %26ndash; web.config

%26nbsp;

 1 %26lt;basicHttpBinding%26gt; 2  3         %26lt;binding name="b0"%26gt; 4  5           %26lt;security mode="TransportWithMessageCredential"%26gt; 6  7             %26lt;message clientCredentialType="Certificate"/%26gt; 8  9           %26lt;/security%26gt;10 11         %26lt;/binding%26gt;

%26nbsp;

%26nbsp;

Client %26ndash; app.config

%26nbsp;

 1 %26lt;bindings%26gt; 2  3             %26lt;basicHttpBinding%26gt; 4  5                 %26lt;binding name="BasicHttpBinding_IService1"%26gt; 6  7                     %26lt;security mode="TransportWithMessageCredential"%26gt; 8  9                       %26lt;message clientCredentialType="Certificate"/%26gt;10 11                     %26lt;/security%26gt;12 13                 %26lt;/binding%26gt;14 15             %26lt;/basicHttpBinding%26gt;16 17         %26lt;/bindings%26gt;18 19         %26lt;client%26gt;20 21             %26lt;endpoint address="https://server2/TransportMessageService/Service1.svc"22 23                 binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1" behaviorConfiguration="cl"24 25                 contract="ServiceReference1.IService1" name="BasicHttpBinding_IService1" /%26gt;26 27         %26lt;/client%26gt;28 29       %26lt;behaviors%26gt;30 31         %26lt;endpointBehaviors%26gt;32 33           %26lt;behavior name="cl"%26gt;34 35             %26lt;clientCredentials%26gt;36 37               %26lt;clientCertificate findValue="31 7c d9 40 b5 ac b0 8e 99 99 00 00 12"38 39                   storeLocation="LocalMachine"40 41                   storeName="My"42 43                   x509FindType="FindBySerialNumber"/%26gt;44 45             %26lt;/clientCredentials%26gt;46 47  48 49           %26lt;/behavior%26gt;50 51         %26lt;/endpointBehaviors%26gt;52 53       %26lt;/behaviors%26gt;

%26nbsp;

%26nbsp;

l%26nbsp;%26nbsp; wsHttpBinding %26ndash; Transport %26ndash; Ntlm

%26nbsp;

Service %26ndash; web.config

%26nbsp;

 1 %26lt;wsHttpBinding%26gt; 2  3                 %26lt;binding name="wsTransportBinding"%26gt; 4  5                     %26lt;security mode="Transport"%26gt; 6  7                         %26lt;transport clientCredentialType="Ntlm" /%26gt; 8  9                     %26lt;/security%26gt;10 11                 %26lt;/binding%26gt;12 13             %26lt;/wsHttpBinding%26gt;14 15  16 17 Client %26ndash; app.config18 19  20 21 %26lt;bindings%26gt;22 23             %26lt;wsHttpBinding%26gt;24 25                 %26lt;binding name="WSHttpBinding_IService1"%26gt;26 27                     %26lt;security mode="Transport"%26gt;28 29                         %26lt;transport clientCredentialType="Ntlm" /%26gt;30 31                     %26lt;/security%26gt;32 33                 %26lt;/binding%26gt;34 35             %26lt;/wsHttpBinding%26gt;36 37         %26lt;/bindings%26gt;38 39         %26lt;client%26gt;40 41             %26lt;endpoint address="https://server/TransportMessageService/Service1.svc"42 43                 binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1"44 45                 contract="ServiceReference1.IService1" name="WSHttpBinding_IService1"%26gt;46 47                 %26lt;identity%26gt;48 49                     %26lt;servicePrincipalName value="host/pupanda-server.fareast.corp.microsoft.com" /%26gt;50 51                 %26lt;/identity%26gt;52 53             %26lt;/endpoint%26gt;54 55         %26lt;/client%26gt;

%26nbsp;

%26nbsp;

%26nbsp;

参考文档

%26nbsp;

http://msdn.microsoft.com/en-us/library/vstudio/hh273122(v=vs.100).aspx

http://blogs.msdn.com/b/socaldevgal/archive/2007/08/15/why-is-wshttpbinding-secure-by-default-how-does-it-work.aspx

http://stackoverflow.com/questions/1683724/what-are-the-impacts-of-setting-establishsecuritycontext-false-if-i-use-https

http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512/ws-secureconversation-1.3-os.html

0 0