Solved

.Net 2.0 to .Net 4.0 Conversion Issue

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
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
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

628 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