从其他 wcf 服务调用 wcf 服务时传递窗口凭据

本文关键字:服务 wcf 窗口 其他 调用 | 更新日期: 2023-09-27 17:55:21

我有以下问题:

我有两个WCF服务,都使用基本的HttpBinging,定义为:

    <binding name="DefaultBasicBinding" closeTimeout="01:00:00" openTimeout="01:00:00"
      receiveTimeout="01:00:00" sendTimeout="01:00:00" maxReceivedMessageSize="2147483647">
      <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" />
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows"/>
      </security>
    </binding>

在web.config中,我也有这样的行:

< authentication mode="Windows"/>
< serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

然后我有调用ServiceNo1的silverlight应用程序。在服务器端,执行服务方法。在她的身体中,ServiceNo1和ServiceNo2作为客户端被调用。

ServiceNo1 client1 = new ServiceNo1();
client1.ExecuteNextMethod1();

ServiceNo2 client2 = new ServiceNo2();
client2.ExecuteNextMethod2();    

这在 locahost 上非常有效。当这在 dev 上发布时 - 问题开始了。

在 silverlight 应用程序调用的方法中执行服务方法时,将引发异常:

Exception type: 
System.ServiceModel.Security.MessageSecurityException  
Message: 
The HTTP request is unauthorized with client authentication scheme 'Negotiate'. 
The authentication header received from the server was 'Negotiate,NTLM'. 

看起来没有传递Windows凭据。

上面列出的设置也在开发中。两台服务器(localhost和dev)都在IIS上设置了"仅Windows身份验证"。

有人帮不了我吗?

从其他 wcf 服务调用 wcf 服务时传递窗口凭据

最有可能发生的事情是,在本地主机上,您的网络服务器正在使用您的凭据运行。在开发服务器上,Web 服务器将在不同的服务帐户(例如应用程序的 IIS 应用程序池服务)下运行。

最好的解决方案是获取应用程序正在运行的服务帐户,以针对第二个服务进行授权。

还有其他选择 - 在 dev 中使用您的用户凭据作为服务帐户(我假设这在受控环境中不是一个选项),或者在服务器端模拟您的凭据(再次 - 这不太可能适合受控环境)。

编辑最适合您的选择可能是模拟用户的凭据。有关实现,请参阅此 MSDN 文章。我对 Silverlight 不太熟悉,但您应该能够执行以下操作:

using System.Security.Principal;
------------------------
WindowsImpersonationContext impersonationContext;
impersonationContext = 
    ((.WindowsIdentity)HttpContext.User.Identity).Impersonate();
//Call your service here.
impersonationContext.Undo();

请注意,您可能会遇到双跃点问题,其中Windows不允许您将用户凭据从一台服务器传递到另一台服务器 - 我会阅读链接的文章以获取背景。