WCF和Windows身份验证

时间:2010-06-21 16:46:59

标签: c# wcf

我正在尝试使用Windows身份验证和IIS 7在我们的服务器上设置WCF服务。调用该服务时,我收到以下错误消息。

无法打开登录请求的数据库“TestDB”。登录失败。 用户'NT AUTHORITY \ NETWORK SERVICE'登录失败。

这是我的WCF服务的配置文件。

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
      <customErrors mode="Off"/>
      <authentication mode="Windows"/>
    </system.web>

    <system.serviceModel>
      <bindings>
        <basicHttpBinding>
          <binding name="binBulletin">
            <security mode="TransportCredentialOnly">
              <transport clientCredentialType="Windows" />
            </security>
          </binding>
        </basicHttpBinding>
      </bindings>
      <services>
        <service behaviorConfiguration="BulletinBoardService.BulletinBehavior"
          name="BulletinBoardService.BulletinService">
          <endpoint address="" binding="basicHttpBinding" bindingConfiguration="binBulletin"
            name="epBulletin_Basic" contract="BulletinBoardService.IBulletinService">
            <identity>
              <dns value="localhost" />
            </identity>
          </endpoint>
          <endpoint address="mex" binding="basicHttpBinding" bindingConfiguration="binBulletin"
            name="epBulletin_Mex" contract="IMetadataExchange" />
        </service>
      </services>
      <behaviors>
        <serviceBehaviors>
          <behavior name="BulletinBoardService.BulletinBehavior">
            <serviceMetadata httpGetEnabled="true" />
            <serviceDebug includeExceptionDetailInFaults="true" />
            <serviceAuthorization impersonateCallerForAllOperations="true" />
          </behavior>
        </serviceBehaviors>
      </behaviors>
      <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
    <system.webServer>
      <security>
        <authentication>
          <windowsAuthentication enabled="true"/>
        </authentication>
      </security>
    </system.webServer>
</configuration>

任何提示都将不胜感激。

2 个答案:

答案 0 :(得分:2)

我不知道这会对任何人有所帮助,但我遇到了这个问题。问题是由于我的服务在IIS中使用的应用程序池的标识设置。如果你在那里设置了适当的身份,你应该是好的。在我的例子中,默认设置为NT Authority \ Network Service。

答案 1 :(得分:1)

如果您使用本地SQL Server或NT AUTHORITY\NETWORK SERVICE(其中AspNetServer是运行IIS7的服务器的名称),您应该在SQL Server中为DomainName\AspNetServer$帐户授予访问权限。有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/ff647402.aspx的最后一部分。

UPDATED:首先,您不要忘记配置IIS的虚拟目录以打开“Windows身份验证”并关闭“匿名身份验证”。

我不确定basicHttpBinding是否是您的最佳选择。你几乎没有写过你的WCF服务,所以我不推荐你。例如,查看http://msdn.microsoft.com/en-us/library/ms734769.aspx以了解您可以选择的方式。

要访问本地数据库,您可以使用用户模拟。它通常可以根据WFC调用实现,也可以在访问DB之前进行临时模拟。 (有关详细信息,请参阅http://www.danrigsby.com/blog/index.php/2008/04/17/impersonate-a-clients-identity-in-wcf/)。

主要问题是从服务访问数据库没有最佳方式。例如,如果您选择模拟。它看起来很不错,因为在数据库内部可以看到请求来自哪个用户。但在很多实际情况下,模仿的使用并不能真正解决问题,而只是转发它。谁应该管理数据库内的权限?关于哪个工具应该授予用户许可。谁在您的公司进行用户管理?在Active Directory中只进行一次用户管理,还是管理SQL Server数据库?因此,由于您公司的现有流程,用户模仿可能不是最佳选择。

从WCF访问数据库有很多不同的方案。例如,在我实现的最后一个项目中,我写了一个WCF服务,它有很多方法。在方法开头的每个方法内部,我使用Microsoft授权管理器API来验证用户是否具有相应操作的权限。其中一个使用授权管理器管理单元通过将其分配给某个应用程序角色来向用户授予权限。为了访问数据库,我使用了一个用户帐户(例如NT AUTHORITY\NETWORK SERVICE)。然而,解决方案是安全的,并且有一个用户管理工具,它符合业务要求。

我对您的环境和业务要求的信息太少,无法给您一个建议。我只想说明你可能需要根据你拥有的不同可能性制定解决方案的安全概念。