如何将部署在 IIS 和远程客户端上的 WCF 服务配置为从远程客户端电脑进行身份验证

本文关键字:客户端 配置 服务 身份验证 电脑 WCF 部署 IIS | 更新日期: 2023-09-27 18:36:55

我是菜鸟;请帮助我理解这个让我非常困惑的身份验证配置/绑定的东西。

我在 Win 2008 上的 IIS 7 上部署了一个 C# WCF 服务。 我的客户端是一个 Windows 窗体 C# 应用程序。 我的客户端在从运行 WCF 服务的同一服务器运行时工作正常,但是当我尝试从远程 PC 运行客户端时,出现以下异常...

System.ServiceModel.Security.SecurityNegotiationException: 调用方未通过服务身份验证。

我已经阅读了一些有关这些问题的文章,并且知道我的问题是因为我的服务和客户端配置为使用 Windows 身份验证,我想这是使用 Visual Studio 创建服务并将服务引用添加到客户端时的默认身份验证。 以下是我在进行任何更改之前的配置,当时它仍然设置为 Windows(删除了不相关的位)......

网络配置

<system.web>
    ...
    <authentication mode="Windows"/>
    ...
<system.serviceModel>
    <services>
        <service name="MCLaborServer.LaborService" behaviorConfiguration="MCLaborServer.LaborServiceBehavior">
            <!-- Service Endpoints -->
            <endpoint address="" binding="wsHttpBinding" contract="MCLaborServer.ILaborService">
                <!-- 
          Upon deployment, the following identity element should be removed or replaced to reflect the 
          identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
          automatically.
      -->
                <identity>
                    <dns value="localhost"/>
                </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="MCLaborServer.LaborServiceBehavior">
                <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
                <serviceMetadata httpGetEnabled="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>
</system.serviceModel>

从客户端上的应用程序配置...

<system.serviceModel>
    <bindings>
        <wsHttpBinding>
            <binding name="WSHttpBinding_ILaborService" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                allowCookies="false">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <reliableSession ordered="true" inactivityTimeout="00:10:00"
                    enabled="false" />
                <security mode="Message">
                    <transport clientCredentialType="Windows" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="Windows" negotiateServiceCredential="true"
                        algorithmSuite="Default" establishSecurityContext="true" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://<myDnsNameGoesHere>/MCLaborServer/LaborService.svc"
            binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ILaborService"
            contract="LaborService.ILaborService" name="WSHttpBinding_ILaborService">
            <identity>
                <dns value="localhost" />
            </identity>
        </endpoint>
    </client>
</system.serviceModel>
因此,首先我在web.config中更改了"身份验证模式="None",

并在客户端的app.config中设置了"安全模式="None",并为消息和传输设置了clientCredentialType="None"。 我还注释掉了web.config和客户端app.config中的"身份"部分。 这完全破坏了它,现在本地运行的客户端甚至无法工作;它给出"远程服务器返回意外响应: (405) 方法不允许"错误。

那么,我

该怎么做才能关闭安全性,以便我可以使用远程客户端进行连接呢? 我确实在 IIS 中为我的应用程序启用了匿名访问。

我还想问,配置它的最佳实践方法是什么,这样我就可以以半安全的方式在互联网上通过远程客户端进行 Web 服务调用,而无需使用 SSL 或做任何会花钱的事情。 我并不真正关心数据的安全性,因为它不是真正的敏感数据,但我仍然想确保服务器不会受到攻击。

此外,我读到我可以使用 Windows 身份验证,然后在代码中显式指定凭据,如下所示。 如果我这样做,它还能远程工作吗? 如果是这样,这最终是否会使我的服务器Windows凭据以不安全的方式通过网络发送,那么我就可以接受我的凭据被劫持吗?

SomeService.ServiceClient someService = new SomeService.ServiceClient(); 
someService.ClientCredentials.Windows.ClientCredential.UserName="windowsuseraccountname" 
someService.ClientCredentials.Windows.ClientCredential.Password="windowsuseraccountpassword"

我已经阅读了以下帖子/链接,但仍然感到困惑。 感谢您的任何帮助!

WCF 错误:服务未对调用方进行身份验证

如何解决"调用方未通过服务身份验证"?

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

http://www.devx.com/codemag/Article/33342/1763/page/2

如何将部署在 IIS 和远程客户端上的 WCF 服务配置为从远程客户端电脑进行身份验证

在设置跨域运行的低安全性 WCF 服务时,我们遇到了类似的问题。最大的问题之一(如果可以这么称呼它)是默认情况下 WCF 配置为非常安全。由于我们的应用程序完全位于安全网络中,因此我们不想为许多复杂的证书而烦恼。我们的解决方法是创建自定义绑定,允许我们对服务使用用户名/密码身份验证,而无需任何加密。我们的实现基于 Yaron Naveh 的清除用户名绑定。我建议你看看它(以及他的博客文章介绍它)。

了解 WCF 绑定和安全性的一些很好的资源:

  • MSDN - Windows Communication Foundation Bindings 概述
  • MSDN - 系统提供的绑定
  • MSDN - 安全概述
  • MSDN - 对 WCF 安全性进行编程
  • MSDN - WCF 安全基础
  • MSDN - 选择传输

我通过将绑定更改为basicHttpBinding,将身份验证更改为Forms并关闭安全性来解决此问题。