Solved

.Net 2.0 to .Net 4.0 Conversion Issue

Posted on 2011-03-12
1
428 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
1 Comment
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 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

22 Experts available now in Live!

Get 1:1 Help Now