Solved

Thread.CurrentPrincipal Casting Problem

Posted on 2011-03-11
5
311 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
  • 3
  • 2
5 Comments
 
LVL 74

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 74

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 74

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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 is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now