如何禁用wcf服务的安全性?

时间:2014-07-23 12:42:39

标签: c# wcf net.tcp

我已经在app.config中编写了一个带有以下配置的WCF应用程序:

<system.serviceModel>
    <services>
        <service name="ProxyService">
            <endpoint address="net.pipe://localhost/ProxyService" binding="netNamedPipeBinding" bindingConfiguration="" name="ServiceProxy" contract="IProxyService"/>
        </service>
      <service name="PublishSubscribeService">
        <endpoint address="net.tcp://localhost/PublishSubscribeService" binding="netTcpBinding" bindingConfiguration="" name="PublishSubscribeServer" contract="IPublishSubscribeService"/>
      </service>
    </services>
</system.serviceModel>

我遇到了netTcp绑定拒绝请求的问题(我已经包含了其他服务,以防万一它可能会产生影响,但我不认为它会这样做)。我使用以下代码实例化它:

private ServiceHost incomingPipeHost;
private ServiceHost incomingSubscribeHost;

incomingPipeHost = new ServiceHost(typeof(ProxyService));
incomingPipeHost.Open();

incomingSubscribeHost = new ServiceHost(typeof(PublishSubscribeService));
incomingSubscribeHost.Open();

当我尝试从本地运行的客户端连接到net.tcp服务时,这一切都正常,我已经在客户站点上运行了一年多。但是,我现在在不同的客户站点,当我尝试从远程计算机连接时,我得到以下异常:

System.ServiceModel.Security.SecurityNegotiationException: The server has rejected the client credentials. ---> System.Security.Authentication.InvalidCredentialException: The server has rejected the client credentials. ---> System.ComponentModel.Win32Exception: The logon attempt failed
   --- End of inner exception stack trace ---
   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.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.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, String targetName, ProtectionLevel requiredProtectionLevel, TokenImpersonationLevel allowedImpersonationLevel)
   at System.ServiceModel.Channels.WindowsStreamSecurityUpgradeProvider.WindowsStreamSecurityUpgradeInitiator.OnInitiateUpgrade(Stream stream, SecurityMessageProperty& remoteSecurity)
   --- End of inner exception stack trace ---

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)
   at System.ServiceModel.Channels.ServiceChannel.CallOpenOnce.System.ServiceModel.Channels.ServiceChannel.ICallOnce.Call(ServiceChannel channel, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.CallOnceManager.CallOnce(TimeSpan timeout, CallOnceManager cascade)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

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 ....PublishSubscribeServiceLibrary.IPublishSubscribeService.Subscribe(UserData userData)
   at ....PublishSubscribeServiceLibrary.PublishSubscribeClient.Subscribe(UserData userData)
   at ....OnConnected()

我已尝试将以下代码段添加到system.ServiceModel以禁用身份验证,但仍然会收到相同的错误:

<bindings>
    <netTcpBinding>
        <binding name="netTcpBindingConfig" transferMode="Buffered" maxReceivedMessageSize="5242880">
            <readerQuotas maxArrayLength="5242880" />
            <security mode="None" />
        </binding>
    </netTcpBinding>
</bindings>

我做错了什么?我该怎么做才能尝试禁用身份验证(这不是必需的)或让身份验证工作?

1 个答案:

答案 0 :(得分:1)

您需要告诉EndPoint netTcpBinding使用正确的配置(netTcpBindingConfig):

<service name="PublishSubscribeService">
  <endpoint ... bindingConfiguration="netTcpBindingConfig" ... />
</service>