【问题标题】:How do I solve SecurityNegotiationException and InvalidCredentialException in WCF如何解决 WCF 中的 SecurityNegotiationException 和 InvalidCredentialException
【发布时间】:2013-11-30 12:37:47
【问题描述】:

所以我写了一个小游戏,希望这个游戏可以在互联网上玩。

我一直在使用这些配置通过 localhost 测试游戏:

客户端 Winapp 应用程序:

App.Config 文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <system.serviceModel>
    <client>
        <endpoint name ="CommandBoard"
                  address="net.tcp://localhost:9000/commandboard"
                  binding = "netTcpBinding"
                  contract="CommandBoardServiceLibrary.ICommandBoardService"/>
    </client>
  </system.serviceModel>
</configuration>

在客户端 Winform 代码中我有它像这样连接

ChannelFactory<ICommandBoardService> remoteFactory= new ChannelFactory<ICommandBoardService>("CommandBoard");
ICommandBoardService proxy = remoteFactory.CreateChannel();

接下来,为了托管服务,我创建了一个控制台应用程序。

App.config 是基本的。我没有改变任何东西,只是在实际代码中。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

控制台应用代码:

static void Main(string[] args)
{
    using (ServiceHost host = new ServiceHost(typeof(CommandBoardServiceLibrary.CommandBoardService)))
    {
        host.AddServiceEndpoint(typeof(
            CommandBoardServiceLibrary.ICommandBoardService),
            new NetTcpBinding(),
            "net.tcp://localhost:9000/commandboard");
        host.Open();

        Console.ReadLine();
     }
}

同时运行客户端和主机在我的计算机上完美运行。

现在当我将客户端的 App.Config 更改为

<client>
    <endpoint name ="CommandBoard"
              address="net.tcp://23.122.59.211:9000/commandboard"
              binding = "netTcpBinding"
              contract="CommandBoardServiceLibrary.ICommandBoardService"/>
</client>

并在不同网络上的不同计算机上运行它,我收到以下错误: System.ServiceModel.Security.SecurityNegotiationException: The server has rejected the client credentials. ---&gt; System.Security.Authentication.InvalidCredentialException: The server has rejected the client credentials. ---&gt; System.ComponentModel.Win32Exception: The logon attempt failed

这个错误的根源在哪里?是否配置不正确?还是我错过了什么?

编辑:WCF 服务的 Web.config

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

【问题讨论】:

  • 服务器的 WCF 配置是什么?
  • @Szymon 我怎样才能为您找到它?我刚开始学习WCF,所以对所有东西的布局都不太习惯。您是否在我的 CommandBoardServiceLibrary 中寻找 Web.Config 文件?
  • 它是定义安全要求的服务器。是的,就是服务器的web.config。
  • @Szymon,你还介意帮我吗?

标签: c# wcf exception networking network-programming


【解决方案1】:

根据您的配置以及它确实在本地工作的事实,这意味着您已正确配置了一部分。但是,您应该为您的主机部分执行此操作。

<endpoint address= ""
     binding="netTcpBinding"
     bindingConfiguration= ""
     name= "NetTcpBindingEndpoint"
     contract="Reference.To.Your.Interface.IMyService">

     <identity>
         <dns value="localhost" />
     </identity>
</endpoint>

现在是重要部分之一,您需要在物理上公开您的元数据。如果没有配置该端点,您将遇到严重的问题。

<endpoint address="mex"
     binding="mexTcpBinding"
     bindingConfiguration= ""
     name="MexTcpBindingEndpoint"
     contract="IMetadataExchange" />

<host>
    <baseAddress>
         <add baseAddress="net.tcp://localhost:8523/WcfTestService" />
    </baseAddress>
</host>

现在您已经配置了主机的该部分,您需要实际安装该服务。您只需发布、构建、安装可执行文件,然后通过 Services

验证服务确实在您的机器上运行

这是我认为您遇到问题的另一部分,通过svc utility。您无法为您的服务生成有效的URI。如果您已正确配置 Host,这将起作用:

  1. 在您的客户端应用程序中单击鼠标右键
  2. 添加服务参考
  3. URI 设置为:net.tcp://localhost:8523/ServiceName,然后单击Go。它应该可以解决,或者您可以尝试自动解决。端口号可能需要更改。

获得参考后,它应该很简单:

  WCFTestService.MyServiceClient myService =
     new WCFTestService.MyServiceClient();
  MessageBox.Show(myService.DoWork("Hello World!"));
  myService.Close();

您创建Client 对象,然后从接口公开该方法。这种方法还应该缓解这些安全问题。我有一种感觉是因为使用了无效的 URI。

您可能还需要以管理员身份运行 Visual Studio。

希望对你有帮助。

【讨论】:

    【解决方案2】:

    WCF 的所有配置都在&lt;system.serviceModel&gt; 标记内完成。主机和客户端都是如此。由于您的主机服务没有 serviceModel 标签,WCF 将为 netTcpBinding 应用默认值,即传输安全性。

    客户端有服务模型标签,但没有安全设置,所以也会尝试默认的netTcpBinding。

    您可以使用 Visual Studio 附带的“WCF 服务配置编辑器”工具为您的客户端和主机设置绑定和安全性,但基本上,您需要覆盖默认值。

    在客户端和服务器上为端点创建配置,然后 bindingconfiguration 等于 "",像这样:

     <endpoint address=""
               binding="netTcpBinding"
               bindingConfiguration=""
               name="NetTcpBindingEndpoint"
               contract="WCFHostService.IMyService">
    

    Here 你可以找到完整的 netTcpBinding 指南。

    【讨论】:

    • 谢谢。我会尝试一下,看看是否能解决我的问题。但我有一个问题要问你:我在主机控制台中使用的代码不会做完全相同的事情吗? host.AddServiceEndpoint(typeof(CommandBoardServiceLibrary.ICommandBoardService),new NetTcpBinding(),"net.tcp://localhost:9000/commandboard");
    • 注意到了。您应该删除 addEndpoint 代码行。 WCF 将从配置中获取所有端点配置,但您必须让它这样做。
    猜你喜欢
    • 2011-01-09
    • 2011-04-13
    • 2016-04-22
    • 1970-01-01
    • 2014-10-01
    • 2012-08-21
    • 2011-02-16
    • 1970-01-01
    • 2011-02-08
    相关资源
    最近更新 更多