Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

.Net 2.0 to .Net 4.0 Conversion Issue

Posted on 2011-03-12
1
Medium Priority
?
437 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 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

877 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