0

i am having no problem at all compiling/debugging my web app, but when i try to run it from my IIS server i get this error:

Server Error in '/' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   EnterData.Global.Application_AuthenticateRequest(Object sender, EventArgs e) in C:\Users\agordon\Documents\Visual Studio 2008\Projects\lomdb\EnterData\Global.asax.cs:30
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +79
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +170

Version Information: Microsoft .NET Framework Version:2.0.50727.5448; ASP.NET Version:2.0.50727.5420

i have already done:

aspnet_regiis -ua
aspnet_regiis -i

and i have restarted the computer

it is windows 7, IIS 7

the webapp works fine on a different computer

how can i solve this issue? what am i doing wrong?

on line 30 from the stacktrace i have (string username......):

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
    string username = HttpContext.Current.User.Identity.Name.ToString();

    bool yesno = HttpContext.Current.User.IsInRole("Group0Users");

    string role = GetRole.OfUser(username);

}

here is the web.config file:

<?xml version="1.0"?><configuration>

    <configSections>
        <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
                    <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
                    <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                    <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
                    <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>


                </sectionGroup>
            </sectionGroup>
        </sectionGroup>
    </configSections>

  <location path="default.aspx">
    <system.web>
      <authorization>
        <allow roles="mlabs\Group2Users"/>
        <deny users="*"/>
      </authorization>
    </system.web>
  </location>

  <appSettings>
    <add key="dePracticeErrors" value="agordon,user1,user2,user3"/>
    <add key="dePreAnalytical" value="user2,mlabs\\agordon,user1,user3"/>
  </appSettings>


  <connectionStrings>
    <add name="qcvalues_testConnectionString" connectionString="Data Source=hermes;Initial Catalog=qcvalues_test;Integrated Security=True" providerName="System.Data.SqlClient"/>
    <add name="LOMConnectionString" connectionString="Data Source=GAIA;Initial Catalog=LOM;Integrated Security=True" providerName="System.Data.SqlClient"/>
    <add name="LOMold" providerName="System.Data.SqlClient"
       connectionString="server=GAIA; database=LOM; Integrated Security=false; user=lomuser; password=M1llenium92127" />
    <add name="LOM" connectionString="Data Source=gaia;Initial Catalog=LOM;Integrated Security=True" providerName="System.Data.SqlClient"/>

  </connectionStrings>
    <system.web>
        <!-- 
            Set compilation debug="true" to insert debugging 
            symbols into the compiled page. Because this 
            affects performance, set this value to true only 
            during development.
        -->
    <roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider"/>


   <authorization>

  </authorization>


  <compilation debug="true">
            <assemblies>
                <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

                <add assembly="System.Web.Extensions.Design, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
                <add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies>
        </compilation>
        <!--
            The <authentication> section enables configuration 
            of the security authentication mode used by 
            ASP.NET to identify an incoming user. 
        -->
  <!--
            The <customErrors> section enables configuration 
            of what to do if/when an unhandled error occurs 
            during the execution of a request. Specifically, 
            it enables developers to configure html error pages 
            to be displayed in place of a error stack trace.

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
        <pages>
            <controls>
                <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            </controls>
        </pages>
        <httpHandlers>
            <remove verb="*" path="*.asmx"/>
            <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
        </httpHandlers>
        <httpModules>
            <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        </httpModules>
    </system.web>
    <system.codedom>
        <compilers>
            <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
                <providerOption name="CompilerVersion" value="v3.5"/>
                <providerOption name="WarnAsError" value="false"/>
            </compiler>
        </compilers>
    </system.codedom>
    <!-- 
        The system.webServer section is required for running ASP.NET AJAX under Internet
        Information Services 7.0.  It is not necessary for previous version of IIS.
    -->
    <system.webServer>
        <validation validateIntegratedModeConfiguration="false"/>
        <modules>
            <remove name="ScriptModule"/>
            <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        </modules>

        <handlers>

            <remove name="WebServiceHandlerFactory-Integrated"/>
            <remove name="ScriptHandlerFactory"/>
            <remove name="ScriptHandlerFactoryAppServices"/>
            <remove name="ScriptResource"/>
            <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        </handlers>
    </system.webServer>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
                <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
                <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>
JimmyPena
  • 8,694
  • 6
  • 43
  • 64
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • which version of .NET framework is your application targeting? – Davide Piras Oct 25 '11 at 16:02
  • @david in the visual studio in the about it says 3.5 – Alex Gordon Oct 25 '11 at 16:03
  • what code do you have in `Application_AuthenticateRequest`? – Brian Driscoll Oct 25 '11 at 16:03
  • please show at least line 30 from Global.asax.cs and the code for `Application_AuthenticateRequest` – Yahia Oct 25 '11 at 16:03
  • @brian protected void Application_AuthenticateRequest(object sender, EventArgs e) { string username = HttpContext.Current.User.Identity.Name.ToString(); bool yesno = HttpContext.Current.User.IsInRole("Group0Users"); string role = GetRole.OfUser(username); } – Alex Gordon Oct 25 '11 at 16:03
  • @I__ okay, so which of those lines is line 30? That's where your problem is. – Brian Driscoll Oct 25 '11 at 16:04
  • @yahia string username = HttpContext.Current.User.Identity.Name.ToString(); – Alex Gordon Oct 25 '11 at 16:05
  • 1
    This means that either `HttpContext.Current.User` or `HttpContext.Current.User.Identity` is null... you need always to check for such things first! What authentication method have you configured when you deployed this in IIS ? – Yahia Oct 25 '11 at 16:07
  • @yahia i am doing win authentication. it works fine when i compile it from debugger. i posted the web config file – Alex Gordon Oct 25 '11 at 16:09
  • running from debugger doesn't have much to do with running in IIS when it comes to authentication... whatever the reason is that this `null` is a second problem... main problem is that you need to always check first before accessing a variable that is possibly `null` – Yahia Oct 25 '11 at 16:12

3 Answers3

2

Look at the stack trace, about 4 lines from the bottom. It says it hit an error at Global.asax.cs on line 30. Whatever the code is on that line is, it's trying to work with data from a variable that has a null value.

You'll need to find out why that variable is null, and fix the underlying problem. Feel free to post that code into this question so that we can actually help you troubleshoot.

Edit: From looking at your code, you need to set up IIS authentication on this server. Set Authentication to Windows, and uncheck "allow anonymous access".

Jordan
  • 31,971
  • 6
  • 56
  • 67
  • thank i updated with the code that i have there. it is the FIRST line (string username........ – Alex Gordon Oct 25 '11 at 16:05
  • @|__ Updated my answer with some relevant info. – Jordan Oct 25 '11 at 16:09
  • how do i do this?> Edit: From looking at your code, you need to set up IIS authentication on this server. Set Authentication to Windows, and uncheck "allow anonymous access". – Alex Gordon Oct 25 '11 at 16:12
  • This has some helpful answers: http://stackoverflow.com/questions/436169/iis7-setup-integrated-windows-authentication-like-in-iis6 – Jordan Oct 25 '11 at 16:15
1

It's most likely the case that the ToString() call is throwing the null reference exception.

So, start by working backwards and check whether HttpContext.Current.User.Identity.Name is null. If it is, keep working backwards until you get to the first non-null. The property that you stripped last is the one where you need a null check.

For instance, let's say that HttpContext.Current.User is non-null (which it hopefully is). Then you need a null check on Identity in your code:

if(HttpContext.Current.User.Identity != null)
{
   string username = HttpContext.Current.User.Identity.Name.ToString();
   //rest of your code...
}

HTH

Brian Driscoll
  • 19,373
  • 3
  • 46
  • 65
1

change your code to

if ( HttpContext.Current.User != null )
{
     if ( HttpContext.Current.User.Identity != null )
          {
          string username = HttpContext.Current.User.Identity.Name.ToString();

          bool yesno = HttpContext.Current.User.IsInRole("Group0Users");

          string role = GetRole.OfUser(username);
          }

}

BTW : the code in GetRole.OfUser needs to check for null too !

Yahia
  • 69,653
  • 9
  • 115
  • 144