.NET 远程处理有类似 OnClientConnected 事件之类的东西吗?

发布于 2024-08-04 15:21:05 字数 392 浏览 4 评论 0原文

我在我的服务器上使用类似的东西:

TcpServerChannel channel = new TcpServerChannel(settings.RemotingPort);
ChannelServices.RegisterChannel(channel, true);
RemotingServices.Marshal(myRemoteObject, "myRemoteObject");

我想订阅某种事件,以便每当远程客户端连接到 myRemoteObject 时,我都可以检查 Thread.CurrentPrincipal.Identity.Name 来决定是否授权他。

目前我正在 myRemoteObject 的每个公开的远程方法中进行授权检查,这是一个混乱的......

I'm using something like this on my server:

TcpServerChannel channel = new TcpServerChannel(settings.RemotingPort);
ChannelServices.RegisterChannel(channel, true);
RemotingServices.Marshal(myRemoteObject, "myRemoteObject");

I would like to subscribe to some kind of event so that whenever a remote client connects to myRemoteObject, I can check the Thread.CurrentPrincipal.Identity.Name to decide whether to authorize him.

Currently I'm doing the authorizing check in every exposed remote method of myRemoteObject which is a messy...

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

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

发布评论

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

评论(2

∝单色的世界 2024-08-11 15:21:05

在我的远程处理应用程序中,我定义了一个特殊的对象/接口,客户端首先需要授权。如果客户端成功授权远程对象,则特殊对象返回。所以你在一处获得授权。

它看起来像这样。

public interface IPortal
{
  object SignIn(string name, string password);
}

public class Portal : MarshalByRefObject, IPortal
{
  private object _remoteObject;

  public Portal() {
    _remoteObject = new RemoteObject();
  }

  public object SignIn(string name, string password) 
  {
    // Authorization
    // return your remote object

    return _remoteObject;
  }
}

在您的应用程序中,您托管 Portal-Object

TcpServerChannel channel = new TcpServerChannel(settings.RemotingPort);
ChannelServices.RegisterChannel(channel, true);
Portal portal = new Portal()
RemotingServices.Marshal(portal , "portal");

In my remoting application i defined a special object/interface where clients first need to authorize. The special object then returns, if the client successfully authorized the remote object. So you have the authorization at one place.

It looks something like this.

public interface IPortal
{
  object SignIn(string name, string password);
}

public class Portal : MarshalByRefObject, IPortal
{
  private object _remoteObject;

  public Portal() {
    _remoteObject = new RemoteObject();
  }

  public object SignIn(string name, string password) 
  {
    // Authorization
    // return your remote object

    return _remoteObject;
  }
}

In your application you host the Portal-Object

TcpServerChannel channel = new TcpServerChannel(settings.RemotingPort);
ChannelServices.RegisterChannel(channel, true);
Portal portal = new Portal()
RemotingServices.Marshal(portal , "portal");
三生池水覆流年 2024-08-11 15:21:05

您可以使用类似 PostSharp 的内容来从每个方法中提取检查 - 只需在 AOP 建议中执行即可。 (您将其应用于公开其方法的类,而不是客户端连接。)

这种方法独立于您用于远程处理的任何传输 - 它只是排除了远程中所有方法的授权横切关注点班级。

You could use something like PostSharp to factor out the check from every method - just do it in the AOP advice. (You apply this to the class which is exposing its methods, not to the client connection.)

This approach is independent of whatever transport you use for remoting - it just factors out the cross-cutting concern of authorization across all the methods in your remoted class.

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