我在使用WCF的客户端中遇到了安全协商例外

问题描述

| 尝试使用WCF客户端和服务器登录另一台PC上的服务器时,客户端部分出现安全协商异常。它可以在局域网中工作,但是当我使用端口转发和静态IP地址在Internet上使服务器联机时,我在客户端中遇到此异常。 客户端配置:
<?xml version=\"1.0\"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy=\"true\">
  <supportedRuntime version=\"v4.0\" sku=\".NETFramework,Version=v4.0\"/>
  </startup>
    <system.serviceModel>
      <bindings>
        <netTcpBinding>
          <binding name=\"TcpBinding\" closeTimeout=\"00:03:00\" openTimeout=\"00:03:00\"
              receiveTimeout=\"00:10:00\" sendTimeout=\"00:03:00\" transactionFlow=\"false\"
              transferMode=\"Buffered\" transactionProtocol=\"OleTransactions\"
              hostNameComparisonMode=\"StrongWildcard\" listenBacklog=\"10\"
              maxBufferPoolSize=\"524288\" maxBufferSize=\"65536\" maxConnections=\"30\"
              maxReceivedMessageSize=\"65536\">
            <readerQuotas maxDepth=\"32\" maxStringContentLength=\"8192\" maxArrayLength=\"16384\"
                maxBytesPerRead=\"4096\" maxNaMetableCharCount=\"16384\" />
            <reliableSession ordered=\"true\" inactivityTimeout=\"00:30:00\"
                enabled=\"false\" />
            <security mode=\"Transport\">
              <transport clientCredentialType=\"Windows\" protectionLevel=\"EncryptAndSign\">
                <extendedProtectionPolicy policyEnforcement=\"Never\" />
              </transport>
              <message clientCredentialType=\"Windows\" />
            </security>
          </binding>
        </netTcpBinding>
        <wsDualHttpBinding>
          <binding name=\"HttpBinding\" closeTimeout=\"00:03:00\" openTimeout=\"00:03:00\"
              receiveTimeout=\"00:10:00\" sendTimeout=\"00:03:00\" bypassproxyOnLocal=\"false\"
              transactionFlow=\"false\" hostNameComparisonMode=\"StrongWildcard\"
              maxBufferPoolSize=\"524288\" maxReceivedMessageSize=\"65536\"
              messageEncoding=\"Text\" textEncoding=\"utf-8\" useDefaultWebProxy=\"true\">
            <readerQuotas maxDepth=\"32\" maxStringContentLength=\"8192\" maxArrayLength=\"16384\"
                maxBytesPerRead=\"4096\" maxNaMetableCharCount=\"16384\" />
            <reliableSession ordered=\"true\" inactivityTimeout=\"00:30:00\" />
            <security mode=\"Message\">
              <message clientCredentialType=\"Windows\" negotiateServiceCredential=\"true\"
                  algorithmSuite=\"Default\" />
            </security>
          </binding>
        </wsDualHttpBinding>
      </bindings>
      <client>
        <endpoint address=\"net.tcp://41.205.115.225:8000/ChatRoom/service\"
            binding=\"netTcpBinding\" bindingConfiguration=\"TcpBinding\"
            contract=\"ChatRoom\" name=\"TcpBinding\">
          <identity>
            <servicePrincipalName value=\"BalaGNisha\\Balaji Nisha\" />
          </identity>
        </endpoint>
        <endpoint address=\"http://41.205.115.225:8001/ChatRoom/service\" binding=\"wsDualHttpBinding\"
            bindingConfiguration=\"HttpBinding\" contract=\"ChatRoom\" name=\"HttpBinding\">
          <identity>
            <servicePrincipalName value=\"BalaGNisha\\Balaji Nisha\" />
          </identity>
        </endpoint>
      </client>
    </system.serviceModel>
</configuration>
这是例外:
System.ServiceModel.Security.SecurityNegotiationException
was unhandled by user code   Message=A
remote side security requirement was
not fulfilled during authentication.
Try increasing the ProtectionLevel
and/or ImpersonationLevel.
Source=mscorlib   StackTrace:
    Server stack trace:
       at System.ServiceModel.Channels.WindowsstreamSecurityUpgradeProvider.WindowsstreamSecurityUpgradeInitiator.OnInitiateUpgrade(Stream stream,SecurityMessageProperty &remoteSecurity)
       at System.ServiceModel.Channels.StreamSecurityUpgradeInitiatorBase.InitiateUpgrade(Stream stream)
       at System.ServiceModel.Channels.ConnectionUpgradeHelper.InitiateUpgrade(StreamUpgradeInitiator upgradeInitiator,IConnection& connection,ClientFramingDecoder decoder,IDefaultCommunicationTimeouts defaultTimeouts,TimeoutHelper& timeoutHelper)
       at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.SendPreamble(IConnection connection,ArraySegment`1 preamble,TimeoutHelper& timeoutHelper)
       at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.DuplexConnectionPoolHelper.AcceptPooledConnection(IConnection connection,TimeoutHelper& timeoutHelper)
       at System.ServiceModel.Channels.ConnectionPoolHelper.EstablishConnection(TimeSpan timeout)
       at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.Onopen(TimeSpan timeout)
       at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
       at System.ServiceModel.Channels.ServiceChannel.Onopen(TimeSpan timeout)
       at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
    Exception rethrown at [0]:
       at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg)
       at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData,Int32 type)
       at System.ServiceModel.ICommunicationObject.Open(TimeSpan timeout)
       at System.ServiceModel.ClientBase`1.System.ServiceModel.ICommunicationObject.Open(TimeSpan timeout)
       at System.ServiceModel.ClientBase`1.open()
       at lectemplete.ChatControl.Chatwindow_Load()
in D:\\Links\\Imagin Cup
2011\\Client\\lectemplete\\Client\\ClientControl.cs:line
50
       at lectemplete.ChatControl.Chat_Control()
in D:\\Links\\Imagin Cup
2011\\Client\\lectemplete\\Client\\ClientControl.cs:line
36
       at BitsOfStuff.InkPadWindow.Intialize_Connection()
in D:\\Links\\Imagin Cup
2011\\Client\\lectemplete\\Windows\\InkPadWindow.xaml.cs:line
326
       at BitsOfStuff.InkPadWindow.Window_Loaded(Object
sender,RoutedEventArgs e) in
D:\\Links\\Imagin Cup
2011\\Client\\lectemplete\\Windows\\InkPadWindow.xaml.cs:line
43
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target,RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source,RoutedEventArgs args,Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender,RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
       at System.Windows.broadcastEventHelper.broadcastEvent(DependencyObject root,RoutedEvent routedEvent)
       at System.Windows.broadcastEventHelper.broadcastLoadedEvent(Object root)
       at MS.Internal.LoadedOrUnloadedOperation.DoWork()
       at System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()
       at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
       at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
       at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback,Object args,Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source,Delegate method,Int32 numArgs,Delegate catchHandler)
       at System.Windows.Threading.dispatcherOperation.InvokeImpl()
       at System.Windows.Threading.dispatcherOperation.InvokeInSecurityContext(Object state)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code,CleanupCode backoutCode,Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback callback,Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,Object state,Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,Object state)
       at System.Windows.Threading.dispatcherOperation.Invoke()
       at System.Windows.Threading.dispatcher.ProcessQueue()
       at System.Windows.Threading.dispatcher.WndProcHook(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Boolean& handled)
       at MS.Win32.HwndSubclass.dispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback,Delegate catchHandler)
       at System.Windows.Threading.dispatcher.InvokeImpl(dispatcherPriority priority,TimeSpan timeout,Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.dispatchMessage(MSG& msg)
       at System.Windows.Threading.dispatcher.PushFrameImpl(dispatcherFrame frame)
       at System.Windows.Threading.dispatcher.PushFrame(dispatcherFrame frame)
       at System.Windows.Window.ShowHelper(Object booleanBox)
       at System.Windows.Window.Show()
       at System.Windows.Window.ShowDialog()
InnerException:
System.Security.Authentication.AuthenticationException
       Message=A remote side security requirement was not fulfilled during
authentication. Try increasing the
ProtectionLevel and/or
ImpersonationLevel.
       Source=System
       StackTrace:
            at System.Net.Security.NegoState.ProcessReceivedBlob(Byte[] message,LazyAsyncResult lazyResult)
            at System.Net.Security.NegoState.StartReceiveBlob(LazyAsyncResult lazyResult)
            at System.Net.Security.NegoState.CheckCompletionBeforeNextReceive(LazyAsyncResult lazyResult)
            at System.Net.Security.NegoState.StartSendBlob(Byte[] message,LazyAsyncResult lazyResult)
            at System.Net.Security.NegoState.CheckCompletionBeforeNextSend(Byte[] message,LazyAsyncResult lazyResult)
            at System.Net.Security.NegoState.ProcessReceivedBlob(Byte[] message,LazyAsyncResult lazyResult)
            at System.Net.Security.NegoState.ProcessAuthentication(LazyAsyncResult lazyResult)
            at System.Net.Security.NegotiateStream.AuthenticateAsClient(NetworkCredential credential,ChannelBinding binding,String targetName,ProtectionLevel requiredProtectionLevel,TokenImpersonationLevel allowedImpersonationLevel)
            at System.Net.Security.NegotiateStream.AuthenticateAsClient(NetworkCredential credential,TokenImpersonationLevel allowedImpersonationLevel)
            at System.ServiceModel.Channels.WindowsstreamSecurityUpgradeProvider.WindowsstreamSecurityUpgradeInitiator.OnInitiateUpgrade(Stream stream,SecurityMessageProperty& remoteSecurity)
       InnerException: System.ComponentModel.Win32Exception
            Message=The network logon Failed
            ErrorCode=-2147467259
            NativeErrorCode=1790
            InnerException:
服务器或客户端部分的问题是否在此例外中?     

解决方法

        那么您的客户端正在通过Internet访问该服务?在这种情况下,您将无法使用Windows安全性。它仅适用于本地网络/相同的Windows域。     ,        对于此异常,应该在服务器和客户端“ 2”上都设置安全模式:
<security mode=\"None\">
    <transport clientCredentialType=\"None\">
    </transport>

    <message clientCredentialType=\"None\" />
</security>
它对我有用,但是我还有一个例外:   套接字连接已中止。这可能是由于处理您的消息时出错或远程主机超出了接收超时,或者是潜在的网络资源问题引起的