在服务上访问wcf客户端身份 [英] accessing wcf client identity on service

查看:162
本文介绍了在服务上访问wcf客户端身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WCF教程之后,我可以开发一个WCF客户端/服务器应用程序,服务和客户端应用程序都是Windows窗体应用程序。我可以通过指定UserName和密码使用每个客户端调用服务。我的WCF服务应用程序还显示所有连接的客户端及其用户名。但是,当多个客户端向服务发送请求时,我无法识别哪个用户调用了该方法。这很重要,因为我的应用程序往往为每个客户端处理自己的会话,正如任何常规的ASP.NET应用程序。每个用户都有自己的身份和自己的应用程序域。



此外,我希望我的服务发送消息回客户端,所以我已实现回调合同。此外,我使用netTcpBinding,因为我的应用程序需要在我的内部网上运行。



如何在WCF客户端/服务器应用程序中实现这个场景?

任何帮助请



感谢






感谢您以前的回复。它真的对我有帮助。
现在,如果我想使用用户名和密码使用自定义验证。
假设我有50个客户端具有有效的用户名和密码。如何获得在特定时间点调用服务方法的客户端(在这50个客户中)?



感谢

解决方案

在您的服务器端代码中,您应该能够从安全上下文中检索调用者的身份:

  if(ServiceSecurityContext.Current!= null& 
ServiceSecurityContext.Current.PrimaryIdentity!= null)
{
string userName = ServiceSecurityContext.Current.PrimaryIdentity.Name;
}

如果您使用Windows身份验证你应该能够访问安全上下文的 .WindowsIdentity (这将是 null 用于任何其他认证机制)。


After couples of WCF tutorials, I could develop a WCF client/Server application, both service and client applications are Windows Forms Application. I can call service using each client by specifying UserName and password. My WCF service applications also shows all the connected clients with their username as well. But, When multiple clients send a request to service then I'm not being able to identity which user has called the method. This is important as my application tend to have its own session for each client processing, just as any regular ASP.NET application has. Each user have their own Identity and its own Application Domain.

Moreover, I want my service to send messages back to client, so I have implemented callback contract. In addition, I'm using netTcpBinding as my applications need to run on my intranet.

How can I implement this scenario in WCF client/server application ?

Any help please ??

Thanks


Thanks for your previous reply. Its really helpful to me. Now, What If I want to use custom authentication using username and password. Lets assume that I have 50 clients with valid username and password. How can I get an identity of a client (out of those 50) whose is invoking a service method at a particular point of time ?

Thanks

解决方案

In your server side code, you should be able to retrieve the caller's identity from the security context - something like:

if(ServiceSecurityContext.Current != null &&
   ServiceSecurityContext.Current.PrimaryIdentity != null)
{
   string userName = ServiceSecurityContext.Current.PrimaryIdentity.Name;
}

If you're calling a service with Windows authentication (which might also work for you - if you're on a corporate LAN, as it would seem) - you should be able to access the security context's .WindowsIdentity instead (this will be null for any other authencation mechanism).

这篇关于在服务上访问wcf客户端身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆