【问题标题】:How to configure WCF service deployed on IIS and remote client to authenticate from remote client PC?如何配置部署在 IIS 和远程客户端上的 WCF 服务以从远程客户端 PC 进行身份验证?
【发布时间】:2012-06-15 23:43:20
【问题描述】:

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

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

System.ServiceModel.Security.SecurityNegotiationException:调用者未经服务验证。

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

Web.Config

<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>

并且从客户端上的 App.Config...

<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 中更改了“authentication mode="None"”,并在客户端的 app.config 中设置了“security mode="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 error: The caller was not authenticated by the service

How to fix "The caller was not authenticated by the service"?

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

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

【问题讨论】:

  • 如果客户端和服务器在同一个域中,或者在相互信任的域中,您只能使用 Windows 身份验证。如果不是,Windows 身份验证将无法工作,因为它取决于两端是否位于同一域或同一系列的受信任域中。

标签: c# wcf authentication iis wshttpbinding


【解决方案1】:

我们在设置跨域运行的低安全性 WCF 服务时遇到了类似问题。最大的问题之一(如果你可以这样称呼它)是 WCF 默认配置为非常安全。因为我们的应用程序完全在一个安全的网络中,所以我们不想为许多复杂的证书而烦恼。我们的解决方法是创建一个自定义绑定,允许我们在没有任何加密的情况下对我们的服务使用用户名/密码身份验证。我们的实现基于Yaron Naveh'sClear Username Binding。我建议你看看那个(以及他的blog post introducing it)。

一些学习 WCF 绑定和安全性的好资源:

【讨论】:

  • 谢谢,这对我来说可能是一个可行的选择。有什么好的参考资料可以让我了解不同的安全类型并查看使用每种安全类型的一些部署方案吗?到目前为止,我发现的所有内容都假设我知道每种安全类型的作用,并假设我掌握了很多我没有的知识。因此,如果您知道的话,我正在寻找初学者的参考。也感谢您的链接,我将尝试清除用户名绑定。
  • @Jim - 我添加了一些在我开始使用 WCF 时对我有帮助的链接。我发现 MSDN 和 Google 上都有关于几乎所有受支持的 Binding 和 Security 组合的操作指南(例如,Transport Security 在此处的几个不同绑定的上下文中进行了解释:msdn.microsoft.com/en-us/library/ms729700.aspx
【解决方案2】:

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

【讨论】:

  • 关闭安全性是什么意思?
猜你喜欢
  • 2012-04-15
  • 2012-09-25
  • 2012-02-06
  • 1970-01-01
  • 2012-12-07
  • 2012-05-13
  • 1970-01-01
  • 2014-08-29
  • 1970-01-01
相关资源
最近更新 更多