Link to home
Start Free TrialLog in
Avatar of wideman1926
wideman1926

asked on

Thread.CurrentPrincipal Casting Problem

The code Thread.CurrentPrincipal as IChatPrinciple) returns null. IChatPrinciple extends IPrincipal, but a conversion error still occurs.  I've been upgrading my application from .net 2.0 to .net 4.0.  The .net 2.0 app did not an issue with the code, and I'm at a loss as to what IIS (version 6) settings could be causing this.  Any help would be much appreciated. Thanks.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wideman1926
wideman1926

ASKER

Thread.CurrentPrincipal is IPrinciple, which is the base class of the class that I'm casting to.  You can cast from a base to a derived class.  Plus, the application that this code is a part of works and is used heavily.  The application will not work if that line of code fails.  It worked fine in 2.0, but is failing in .net 4.0.  That's what's perplexing me.
You're not understanding what I am saying  : )

You are trying to cast from one derived type to another derived type. This cannot be done. You can cast each derived type to IPrinciple, but you cannot cast between two derived types.

Technically speaking, we are talking about casting to an interface, not a base class.
Think of it in this example.


We have a base class Employee. We extend it in two classes:  SalariedEmployee and HourlyEmployee. I can cast a SalariedEmployee to be an Employee, and I can cast an HourlyEmployee to be an Employee. Does it make sense that I should be able to cast a HourlyEmployee to be a SalariedEmployee?
No it doesn't.  I didn't write the code.  I'm just trying to figure out how to make it work in .net 4.0.  Anyway, I'm curious, though,  Why did the code work in the .net 2.0 application?  It gets run constantly, so it's not an issue of it not being reached.