Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

.Net 2.0 to .Net 4.0 Conversion Issue

Posted on 2011-03-12
1
Medium Priority
?
436 Views
Last Modified: 2012-06-27
I'm trying to convert an Asp.net 2.0 application into an Asp.net 4.0 application.  I didn't write the original code and don't have access to the coders.  The line of code: return (Thread.CurrentPrincipal as IChatPrinciple) is not working properly.  I know that you cannot convert between two derived classes, bu the code worked in .net 2.0. In making the upgrade I'd like to know what change between the frameworks could have caused it to fail.  I'd really appreciate the help.  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
1 Comment
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 2000 total points
ID: 35117847

C# 4.0 and the .Net Framework 4.0 is a super-set of C# 2.0/3.0 and .Net 2.0/3.5.  So far as I know anything written for .Net 2.0 should compile without change in 4.0.

I suppose it's possible that your project is missing a reference...?

Although, Thread.CurrentPrinciple by default doesn't return an object that implements IChatPrinciple, so that statement shouldn't work in any version of C#.

You must've had some code somewhere that sets Thread.CurrentPrinciple to an object that does implement IChatPrinciple.  Any idea where that code's supposed to be in your project, and can verify it's still there?

Doesn't work:
using System;
using System.Threading;

interface IChatPrincipal
{
    string ChatName { get; }
}

class ChatPrincipal : IChatPrincipal
{
    public string ChatName
    {
        get { return "Mickey Mouse"; }
    }
}

class Program
{
    static void Main(string[] args)
    {
        IChatPrincipal principal = GetChatPrincipal();
        Console.WriteLine(principal.ChatName);
        Console.ReadKey();
    }

    static IChatPrincipal GetChatPrincipal()
    {
        return Thread.CurrentPrincipal as IChatPrincipal;
    }
}

Open in new window


Does work:
using System;
using System.Threading;
using System.Security.Principal;

interface IChatPrincipal : IPrincipal
{
    string ChatName { get; }
}

class ChatPrincipal : IChatPrincipal
{
    public string ChatName
    {
        get { return "Mickey Mouse"; }
    }

    public IIdentity Identity
    {
        get { return new GenericIdentity(this.ChatName); }
    }

    public bool IsInRole(string role)
    {
        if (role.ToLower() == "rodent")
            return true;
        else
            return false;
    }
}

class Program
{
    static void Main(string[] args)
    {
        SetChatPrincipal();
        IChatPrincipal principal = GetChatPrincipal();
        Console.WriteLine("Chat name: " + principal.ChatName);
        Console.ReadKey();
    }

    static void SetChatPrincipal()
    {
        Thread.CurrentPrincipal = new ChatPrincipal();
    }

    static IChatPrincipal GetChatPrincipal()
    {
        return Thread.CurrentPrincipal as IChatPrincipal;
    }
}

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

719 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