tags:

views:

592

answers:

2

I recently set up a new instance of my personal website on the same server as the actual site. The new instance is a "latest" configuration so it's got all the new features of the latest build I'm working on. The latest build I've switched the Target Framework to 3.5 from 2.0 as the only really major change as fas as the compiled items go.

On all member pages and some other pages and some user controls I call one or both of these two items, and each are failing and causing an Object reference not set to an instance of an object, error. But not on every page!

    public static string CurrentUserName
    {
        get
        {
            string userName = "";
            if (HttpContext.Current.User.Identity.IsAuthenticated)
                userName = HttpContext.Current.User.Identity.Name;
            return userName;
        }
    }

    public static string CurrentUserID
    {
        get
        {
            string userID = "";
            if (HttpContext.Current.User.Identity.IsAuthenticated) //Line 39
            {
                MembershipUser user = Membership.GetUser();
                userID = user.ProviderUserKey.ToString();
            }
            return userID;
        }
    }

With the stack trace being:

[NullReferenceException: Object reference not set to an instance of an object.]
   isClasses.BizObjects.get_CurrentUserID() in C:\My Dropbox\Trunk\Classes\BizObjects.cs:39
   TheImageStore_Profile..ctor() in w:\dev\Member\Profile.aspx.cs:9
   ASP.member_profile_aspx..ctor() in c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\faba6118\7249af47\App_Web_profile.aspx.48943a5b.w8t4pvz5.0.cs:0
   __ASP.FastObjectFactory_app_web_profile_aspx_48943a5b_w8t4pvz5.Create_ASP_member_profile_aspx() in c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\faba6118\7249af47\App_Web_profile.aspx.48943a5b.w8t4pvz5.2.cs:0
   System.Web.Compilation.BuildResultCompiledType.CreateInstance() +32
   System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp, Boolean noAssert) +119
   System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath) +33
   System.Web.UI.PageHandlerFactory.GetHandler(HttpContext context, String requestType, String virtualPath, String path) +37
   System.Web.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +307
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

But mind you, this is only happening in IIS 7 on Server 2008, the site is working perfectly from Visual Studio 2008. I've even attached the main website in IIS to this directory and it does the same thing. How is it possible to have this happen, and what could be the cause?

Just for kicks I changed the Target Framework back to 2.0 and it's still causing the same issue. Also this seems to be happening with or without the visitor being logged in, and not just on member pages.

+1  A: 

Either HttpContext.Current.User or user.ProviderUserKey is null.

I would guess the former. Which line is line 39?

Also, is there a user logged in?

SLaks
Yes there is a logged in user. This seems to be from a user control on profile.aspx. But the same user control loads fine on the home page (default.aspx), but again it fails to load on a number of other common pages. Line 39 is `if (HttpContext.Current.User.Identity.IsAuthenticated)`
Tim Meers
Take one of the pages that fail, remove the control, add `<%= HttpContext.Current.User %>` and see what it says.
SLaks
I added it to a page that fails and I get nothing. But when I add it to a page that works correctly it give me `System.Web.Security.RolePrincipal` at the very top of the page. The page that was failing loads correctly with the user controls that use this code are removed.
Tim Meers
Then `HttpContext.Current.User` is `null`. Check `web.config`, and check the request in Fiddler.
SLaks
The issue ended up being in the web.comfig. At some point I added `<modules runAllManagedModulesForAllRequests="true">` to the web.comfig for the site in IIS. Most likely when I moved to IIS 7. Thanks for the help! With out this it was not routing all my requests through the `UrlRewriteModule`.
Tim Meers
A: 

What authentication models have you installed for IIS? By default it is only anonymous that is available and that may set HttpContext.Current.User to null.

Arve
Part of me wants to rule out IIS as the issue because I attached the main website (which is using these same items correctly in existing code) to this directory and it still fails. This is not new code, it's been on the site for a while so it's currently in use already.
Tim Meers
Oh and btw and Forms and Anon are enabled. Forms has a Mode of Use Device Profile
Tim Meers