如何配置部署在IIS和远程客户端上的WCF服务以从远程客户端PC进行身份验证?

时间:2012-06-13 03:10:28

标签: c# wcf authentication iis wshttpbinding

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

我在Win 2008上的IIS 7上部署了C#WCF服务。我的客户端是Windows Forms 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中注释掉了“identity”部分。虽然完全打破了它,现在在本地运行的客户端甚至无法工作;它给出了“远程服务器返回意外响应:(405)方法不允许”错误。

那么我可以做些什么来关闭安全性,以便我可以使用远程客户端进行连接?我确实通过IIS为我的应用程序启用了匿名访问。

我还想问一下,配置这个的最佳实践方法是什么,这样我就可以通过互联网以半安全的方式在远程客户端上进行webservice调用,而无需使用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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

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

相关问题