双工回调始终是匿名的
Posted
技术标签:
【中文标题】双工回调始终是匿名的【英文标题】:Duplex callback is always anonymous 【发布时间】:2010-10-08 08:47:09 【问题描述】:我编写了一个 WCF 双工服务和客户端。在我尝试在客户端实现中调用 .Demand() 之前,一切正常。该服务似乎匿名调用回调方法。我想我错过了如何正确配置服务。
用于创建ServiceHost的代码;
ServiceHost duplex = new ServiceHost(new ServerWCallbackImpl());
NetTcpBinding secureBinding = new NetTcpBinding(SecurityMode.Message);
secureBinding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
duplex.AddServiceEndpoint(typeof(IServerWithCallback),
secureBinding,
"net.tcp://localhost:9080/DataService");
Console.WriteLine(Thread.CurrentPrincipal.Identity.Name); //<-- this correctly shows the current principal
duplex.Open();
if (duplex.State == CommunicationState.Opened)
((ServerWCallbackImpl)duplex.SingletonInstance).Send("Hello World!");
用于创建客户端的代码;
CallbackImpl callbackInstance = new CallbackImpl();
NetTcpBinding secureBinding = new NetTcpBinding(SecurityMode.Message);
secureBinding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
DuplexChannelFactory<IServerWithCallback> cf = new DuplexChannelFactory<IServerWithCallback>(
callbackInstance,
secureBinding,
new EndpointAddress(requestingEndpointAddress));
cf.Credentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
cf.Credentials.Windows.ClientCredential = (NetworkCredential)CredentialCache.DefaultCredentials;
IServerWithCallback srv = cf.CreateChannel(new InstanceContext(callbackInstance));
srv.InitiateConversation();
客户端实现:
public void MethodOnClient(string message)
Console.WriteLine(Thread.CurrentPrincipal.Identity.Name); // <-- anonymous
PrincipalPermission p = new PrincipalPermission(@"DOMAIN\User", null);
p.Demand(); // <-- fails
如何配置以便 ServiceHost 使用 Windows 凭据正确调用回调?
【问题讨论】:
【参考方案1】:是否将 TokenImpersonationLevel 设置为委托而不是模拟?像这样:
cf.Credentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
见this MSDN article。
【讨论】:
掘墓人? :) 2009 年 2 月 18 日提出问题,询问用户因不活动而被删除,我想很久以前。以上是关于双工回调始终是匿名的的主要内容,如果未能解决你的问题,请参考以下文章