ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> DevelopMentor-dotNET
DevelopMentor-dotNET
Re: [DOTNET] Thread.CurrentPrincipal lost after making a call to a remote object?
by Matt Milner other posts by this author
Nov 15 2001 8:24PM messages near this date
Re: [DOTNET] Solved - WAS: Remoting using IIS not working | [DOTNET] Interfaces and Base Classes
As I understand it, this is by design.  There was quite a bit of discussion
on this a while back. See the following link:

http://discuss.develop.com/archives/wa.exe?A2=ind0103D&L=DOTNET&D=0&P=68004



-----Original Message-----
From: Wenbin Zhang [mailto:wzhang@[...].COM]
Sent: Thursday, November 15, 2001 1:33 PM
To: DOTNET@[...].COM
Subject: [DOTNET] Thread.CurrentPrincipal lost after making a call to a
remote object?


Question:
  It seems that Thread.CurrentPrincipal object assigned
  to CurrentThread is lost after making a remote call to an
  object hosted by another process.  Is this behavior by
  design, or it is a bug (I am using Beta2 bits)?
  If the behavior is by design, how to understand this??

I have included some sample code below:
In my sample Client code below, the GenericPrincipal
object assigned to the CurrentThread object is LOST
after making a call to the remote object in the
"Server.exe" process.

Client.cs: (Compile: csc /debug+ /r:Server.exe Client.cs)
-------------------------------------------------------------------
using System;
using System.Threading;
using System.Security.Principal;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Activation;

using TestRemotingNS;

public class RemotingClient
{
    public static void Main()
    {
        string[] roles = {"GBB", "AP"};
        IIdentity alice = new GenericIdentity("alice");
        Thread.CurrentPrincipal = new GenericPrincipal(alice, roles);

        ChannelServices.RegisterChannel(new TcpChannel());
        object[] activationAttributes =
                {new UrlAttribute("tcp://localhost:4242/MyTestService")};

        IPrincipal prin = Thread.CurrentPrincipal;
        string userInfo = "\"" + prin.Identity.Name
                        + "\" is in role \"AP\"?"
                        + prin.IsInRole("AP");
        Console.WriteLine("OSThreadId={0}, CLRThread={1}: {2}",
                       AppDomain.GetCurrentThreadId(),
                       Thread.CurrentThread.GetHashCode(), userInfo);

        // Making an object activation call to a remote server.
        ObjectHandle oh = Activator.CreateInstance("Server",
                                            "TestRemotingNS.TestServer",
                                            activationAttributes);


        prin = Thread.CurrentPrincipal;
        userInfo = "\"" + prin.Identity.Name + "\" is in role \"AP\"? "
                 + prin.IsInRole("AP");
        Console.WriteLine("OSThreadId={0}, CLRThread={1}: {2}",
                        AppDomain.GetCurrentThreadId(),
                        Thread.CurrentThread.GetHashCode(), userInfo);
    }
}

Server.cs: (Compile: csc /debug+ Server.cs)
----------------------------------------------------------------
using System;
using System.Threading;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp;

namespace TestRemotingNS
{
  public class TestServer : MarshalByRefObject
  {
    public string GetInfo()
    {
        string info = "OSThreadId=" + AppDomain.GetCurrentThreadId()
                + ", CLRThread=" + Thread.CurrentThread.GetHashCode();
        return info;
    }
  }

  public class RemotingHost
  {
    static void Main(string[] args)
    {
      RemotingConfiguration.ApplicationName = "MyTestService";

      ActivatedServiceTypeEntry ASTE =
         new ActivatedServiceTypeEntry(typeof(TestRemotingNS.TestServer));

      RemotingConfiguration.RegisterActivatedServiceType(ASTE);

      ChannelServices.RegisterChannel(new TcpChannel(4242));
      Console.ReadLine();
    }
  }
}

Thanks a lot,

Wenbin Zhang
OneSource Information Services, Inc.
300 Baker Avenue
Concord, MA 01742

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved