1

I have created a custom identity, principal and membership provider and accessing them through a UserContext class (all sitting in App_code):

public static class UserContext
{
    public static CustomPrincipal User
    {
        get
        {
            return (CustomPrincipal)HttpContext.Current.User;
        }
    }

    public static CustomIdentity Identity 
    { 
        get 
        {
            return (CustomIdentity)HttpContext.Current.User.Identity; 
        } 
    }
}

From a controller class, I can do:

string userName = UserContext.Identity.Name;

However when doing the following from a view:

 @Html.Encode(UserContext.Identity.Name)

I get the following error at run time:

The type 'Website.Infrastructure.UserContext' exists in both 'c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\533b7079\25c5a39c\assembly\dl3\13397e32\308e247b_c8efce01\Website.DLL' and 'c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\533b7079\25c5a39c\App_Code.-ze7s5wd.dll'

I have cleared out the temp/cache IISExpress folders, etc., ensured that I'm using System.Web v4.0.0, and have tried with adding the using [namespace] at the top of the shared layout view and in the relevant view's Web.config section.

Looking at the view, I'm seeing a compile error (suggestion?) under

UserContext

The type 'xxx' exists both in '[website root]' and 'App_Code' from a MVC view

Suggestions?

Community
  • 1
  • 1
ElHaix
  • 12,846
  • 27
  • 115
  • 203
  • 2
    App_code is a legacy holdover from older versions of asp.net move your code into a class library – Mike Beeler Dec 03 '13 at 02:08
  • 2
    or even just move the class files to a different, non-ASP.NET special folder, like ~/Library/ – ps2goat Dec 03 '13 at 04:10
  • @MikeBeeler - I removed the classes from App_Code, and created a new class library called `Website.Infrastructure`, and put them all in there. Same issue. Suggestions? – ElHaix Dec 04 '13 at 21:32

2 Answers2

0

I wanted to comment but I don't have enough points to comment so giving answer to this.

try to use full qualified class name like

Namespace.UserContext

, this might solve the conflict.

Babu
  • 35
  • 10
  • 1
    Thank you for your comment, however this was also attempted, as you can see from the error, with the fully-qualified class name: `Website.Infrastructure.UserContext`. – ElHaix Dec 03 '13 at 13:55
0

This is your library code in a utility class

Works fine. I notice that in your original description you add references to the views web.config. This is not correct and the views web.config <> to the web.config for the application.

  • add a reference to the utility project to the main web site

  • Build.

In your case take the references out of everywhere else in any of the other config files. I can make the project available to you if you like

Mike Beeler
  • 4,081
  • 2
  • 29
  • 44
  • As this did help, I am marking it as the solution. However I did have a few more issues that were manifested by an A cannot be cast to B exception (http://stackoverflow.com/questions/13119398/a-cannot-be-cast-to-b-exception). I still had the old code in my App_Code folder. Even though I had them set to *Exclude from project* they were for some reason causing this issue. Deleted them, and the `Infrastructure` project now works fine. Thank you. – ElHaix Jan 06 '14 at 22:14