事实证明,我可以使用以下绑定配置对自定义绑定执行我想要的操作:
<customBinding>
<binding name="NewBinding0">
<httpTransport authenticationScheme="Ntlm" keepAliveEnabled="false" />
</binding>
</customBinding>
然后,在客户端,我可以使用以下代码获取运行WCF服务的应用程序池(在IIS中)的标识,以及实际调用WCF服务的用户的标识:
public string GetData(int value)
{
var callingUser = string.Empty;
var appPoolUser = WindowsIdentity.GetCurrent().Name;
var identities =
OperationContext.Current.ServiceSecurityContext.AuthorizationContext.Properties["Identities"] as
IList<IIdentity>;
if (identities != null)
{
var result = from i in identities
where i.AuthenticationType == "NTLM"
select new { i.Name };
if (result.Count() > 0)
{
callingUser = result.First().Name;
}
}
return string.Format("Value Entered: {0}; AppPool User: {1}; Calling User: {2}", value,
appPoolUser, callingUser);
}
我在一个负载平衡的环境中测试了上述代码,以满足需求1,并且一切似乎都运行良好。测试在负载平衡的环境中模拟100个用户的负载10分钟。我们在测试期间关闭了其中一个负载平衡服务器,并且一切都按预期继续运行(即测试期间没有抛出异常,也没有任何标识返回错误)。
上面的代码是关键部分,对于需求2,我丢失了-也就是说,直到这项研究,我才意识到WCF会给你多重身份。
此外,使用此配置,WCF调用的结果不会加密(这是我们想要的)。所以,我认为这种配置对我们的情况很有效。