Solved

.Net 2.0 to .Net 4.0 Conversion Issue

Posted on 2011-03-12
1
425 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

13 Experts available now in Live!

Get 1:1 Help Now