.NETリモート処理用のOnClientConnectedイベントのようなものはありますか?

StackOverflow https://stackoverflow.com/questions/1409239

質問

サーバーで次のようなものを使用しています:

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

リモートクライアントがmyRemoteObjectに接続するたびにThread.CurrentPrincipal.Identity.Nameを確認して、承認するかどうかを判断できるように、何らかのイベントをサブスクライブしたいと思います。

現在、厄介なmyRemoteObjectのすべての公開されたリモートメソッドで認証チェックを行っています...

役に立ちましたか?

解決

リモートアプリケーションで、クライアントが最初に承認する必要がある特別なオブジェクト/インターフェイスを定義しました。クライアントがリモートオブジェクトの認証に成功すると、特別なオブジェクトが返されます。したがって、1か所で承認を取得できます。

このように見えます。

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