双工回调始终是匿名的

时间:2009-02-18 15:02:27

标签: c# wcf duplex servicehost wcf-callbacks

我写了一个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凭据正确调用Callback?

1 个答案:

答案 0 :(得分:0)

是否将TokenImpersonationLevel设置为Delegation而不是Impersonation?像这样:

cf.Credentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;

请参阅this MSDN article