双工回调始终是匿名的

发布于 2024-07-14 00:21:29 字数 1843 浏览 5 评论 0原文

我已经编写了 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 凭据正确调用回调?

I've written a WCF duplex service and client. Everything works well until I try to call .Demand() in the client implementation. It appears that the the service invokes the callback method Anonymously. I think I am missing how to correctly configure the service.

Code used to create 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!");

Code used to create client;

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();

Client implementation:

public void MethodOnClient(string message)
{
    Console.WriteLine(Thread.CurrentPrincipal.Identity.Name);  // <-- anonymous
    PrincipalPermission p = new PrincipalPermission(@"DOMAIN\User", null);
    p.Demand();  // <-- fails
}

How can I configure so that the ServiceHost correctly invokes the Callback with Windows credentials?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赤濁 2024-07-21 00:21:30

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

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

请参阅这篇 MSDN 文章

Does setting TokenImpersonationLevel to Delegation instead of Impersonation? Like this:

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

See this MSDN article.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文