Solved

Thread.CurrentPrincipal Casting Problem

Posted on 2011-03-11
5
327 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:wideman1926
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35112146
You can't cast between two derived types--you can only cast to a common base type (or interface). You should be able to cast Thread.CurrentPrincipal to IPrincipal.
0
 

Author Comment

by:wideman1926
ID: 35112265
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.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35112372
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.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35112401
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?
0
 

Author Comment

by:wideman1926
ID: 35112450
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.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question