繁体   English   中英

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

[英]How to configure WCF service deployed on IIS and remote client to authenticate from remote client PC?

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

我在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为我的应用程序启用了匿名访问。

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

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

有关WCF绑定和安全性的一些很好的资源:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM