首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用WSDualHttpBinding在WCF服务之间共享windows标识

使用WSDualHttpBinding在WCF服务之间共享windows标识
EN

Stack Overflow用户
提问于 2019-08-23 13:38:50
回答 2查看 414关注 0票数 0

我有两个独立托管在IIS 7中的WCF服务。第一个服务可以从外部调用,并使用带有windows身份验证的WebHttpBinding。第二个服务仅由第一个服务调用,使用WsDualHttpBinding

当第一个服务被调用时,我可以从ServiceSecurityContext.Current.WindowsIdentity.Name获得用户的窗口名。在第二个服务中,这是不起作用的,而且ServiceSecurityContext.Current.WindowsIdentity.Name只是IIS APPPOOL\DefaultAppPool

我将WsDualHttpBinding配置为使用windows身份验证,但这没有帮助。以下是服务器端的配置:

代码语言:javascript
复制
<wsDualHttpBinding>
  <binding name="internalHttpBinding">
    <security mode="Message">
      <message clientCredentialType="Windows"/>
    </security>
  </binding>
</wsDualHttpBinding>

下面是第一个服务中与第二个服务建立通信的代码:

代码语言:javascript
复制
private WSDualHttpBinding binding = new WSDualHttpBinding();
private ChannelFactory<IMyService> factory;
public IMyService Contract { get; set; }
public MyServiceCallback Callback { get; set; }

public MyService(Uri uri)
{
    EndpointAddress address = new EndpointAddress(uri);
    Callback = new MyServiceCallback();
    var instanceContext = new InstanceContext(Callback);

    binding.Security.Mode = WSDualHttpSecurityMode.Message;
    binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;

    factory = new DuplexChannelFactory<IMyService>(instanceContext, binding, address);
    factory.Credentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
    factory.Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
    Contract = factory.CreateChannel();

    // Call operations on Contract
}

如何配置第一个服务将用户的身份传递给第二个服务?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-08-24 17:44:58

这似乎是一个通过身份验证的问题。首先,您需要处于Active环境中。必须使用Kerberos进行身份验证,NTLM将无法工作。您可以使用klist检查以下内容:https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/klist

还请参阅https://blogs.msdn.microsoft.com/besidethepoint/2010/05/08/double-hop-authentication-why-ntlm-fails-and-kerberos-works/以获得解释。

也许是这样,所以文章可以帮助:

Pass Windows credentials to remote https WCF service

这个:https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/delegation-and-impersonation-with-wcf

票数 1
EN

Stack Overflow用户

发布于 2019-08-26 09:09:54

在服务器端启用模拟并且客户端设置了windows凭据之后,

代码语言:javascript
复制
ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();
            client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
            client.ClientCredentials.Windows.ClientCredential.UserName = "Test";
            client.ClientCredentials.Windows.ClientCredential.Password = "123456";

我们可以使用下面的代码检索正在运行的Windows帐户。

代码语言:javascript
复制
if (ServiceSecurityContext.Current.WindowsIdentity.ImpersonationLevel == TokenImpersonationLevel.Impersonation ||
    ServiceSecurityContext.Current.WindowsIdentity.ImpersonationLevel == TokenImpersonationLevel.Delegation)
{
    using (ServiceSecurityContext.Current.WindowsIdentity.Impersonate())
    {
        Console.WriteLine("Impersonating the caller imperatively");
        Console.WriteLine("\t\tThread Identity            :{0}",
    WindowsIdentity.GetCurrent().Name);
        Console.WriteLine("\t\tThread Identity level  :{0}",
             WindowsIdentity.GetCurrent().ImpersonationLevel);
        Console.WriteLine("\t\thToken                     :{0}",
             WindowsIdentity.GetCurrent().Token.ToString());
    }
}

请参考下面的例子。

https://learn.microsoft.com/en-us/dotnet/framework/wcf/samples/impersonating-the-client

https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/delegation-and-impersonation-with-wcf

如果有什么需要我帮忙的,请随时通知我。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57627553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档