我在我的服务器上使用这样的东西:

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

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

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

有帮助吗?

解决方案

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

它看起来像这样。

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

其他提示

你可以使用像 PostSharp 这样的东西来计算每种方法的检查 - 只需在AOP建议。 (您将此应用于公开其方法的类,而不是客户端连接。)

这种方法独立于您用于远程处理的任何传输方式 - 它只是考虑了远程类中所有方法的授权的交叉关注。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top