C# call to C++ static class raises NullReferenceException when web.config gets modifided

Discussion in 'ASP .Net' started by AAguiar, Dec 16, 2003.

  1. AAguiar

    AAguiar Guest

    I have an asp.net project where the code behind the aspx page calls a c#
    class which makes calls to a managed static C++ class.
    The C# class works fine when the asp net worker process starts, when it is
    invoked by pressing "F5", or when the web.config file is modified.
    In all these cases the web.config file contains <identity
    impersonate="false" />.

    The mysterious problem arrises when I set <identity impersonate="true"/> in
    the web.config file.
    In this case, each time the web.config file is modified , I get the error
    NullReferenceException: Object reference not set to an instance of an
    object.
    When debugging the error raises at the point where c# class call for the
    first time a function on the static c++ class.

    If I restart de aspnet wroker process the error disappear, until I modify
    again the web.config file.

    Does anyone have any idea of why I get an NullReferenceException at this
    point when I set <identity impersonate="true" /> in the web.config file and
    the aspnet worker process gets recycled ?

    Any help or direction regarding the resolution of this problem would be
    greatly appreciated.

    Thanks in advance.
     
    AAguiar, Dec 16, 2003
    #1
    1. Advertising

  2. Sounds like permissions. With impersonation, if you are using Windows
    Authentication, that user token will be used to invoke that file. If you
    aren't, then the user token for IIS will be used. In either case, you need
    to make sure that the proper user token has access to the necessary
    resources that you are trying to invoke.

    --
    Chris Jackson
    Software Engineer
    Microsoft MVP - Windows Client
    Windows XP Associate Expert
    --
    More people read the newsgroups than read my email.
    Reply to the newsgroup for a faster response.
    (Control-G using Outlook Express)
    --

    "AAguiar" <> wrote in message
    news:%23rafQH$...
    > I have an asp.net project where the code behind the aspx page calls a c#
    > class which makes calls to a managed static C++ class.
    > The C# class works fine when the asp net worker process starts, when it is
    > invoked by pressing "F5", or when the web.config file is modified.
    > In all these cases the web.config file contains <identity
    > impersonate="false" />.
    >
    > The mysterious problem arrises when I set <identity impersonate="true"/>

    in
    > the web.config file.
    > In this case, each time the web.config file is modified , I get the error
    > NullReferenceException: Object reference not set to an instance of an
    > object.
    > When debugging the error raises at the point where c# class call for the
    > first time a function on the static c++ class.
    >
    > If I restart de aspnet wroker process the error disappear, until I modify
    > again the web.config file.
    >
    > Does anyone have any idea of why I get an NullReferenceException at this
    > point when I set <identity impersonate="true" /> in the web.config file

    and
    > the aspnet worker process gets recycled ?
    >
    > Any help or direction regarding the resolution of this problem would be
    > greatly appreciated.
    >
    > Thanks in advance.
    >
    >
    >
     
    Chris Jackson, Dec 16, 2003
    #2
    1. Advertising

  3. AAguiar

    MSFT Guest

    Hi AAguiar,

    Thank you for using Microsoft Newsgroup Service. Based on your description,
    you are dealing with a ASP.NET web application. It seems that you called
    one function in a Managed C++ class when the web application start. The
    application runs well when you set the web.config's impersonate attribute
    as false. However, when change it as ture, you found that there occured an
    "NullReferenceException: Object reference not set to an instance of an
    object" error when starting the web application.( Please correct me if my
    understanding of your issue is not quite exact.)

    If so, here is my suggestion on it:

    1. As for the <identity impersonate="true"/> attribute in the ASP.NET
    web.config file, if you set the authentication mode as "windows" in the
    web.config file for asp.net application. the "impersonte" attribute will
    determine under which account will the asp.net execute code. If you set as
    false. I'll use the default process acount(MACHINE\ASPNET). If you set it
    as true, the workprocess will execute under the account flow from the IIS
    SERVER. Also, you can explicitly specify a certain account for the
    workproecc to execute under.

    here is the description in MSDN:
    Another important security feature is the ability to control the identity
    under which code is executed. Impersonation is when ASP.NET executes code
    in the context of an authenticated and authorized client. By default,
    ASP.NET does not use impersonation and instead executes all code using the
    same user account as the ASP.NET process, which is typically the ASPNET
    account. This is contrary to the default behavior of ASP, which uses
    impersonation by default. In Internet Information Services (IIS) 6, the
    default identity is the NetworkService account.

    you can follow the link:
    http://msdn.microsoft.com/library/en-us/vsent7/html/vxconimpersonation.asp?f
    rame=true
    to get more detailed info on impersonation in ASP.NET.

    So, if you use the "impersonate" function(set it as true in web.config).
    You should make sure that the user accounts which will be impersonated by
    the workprocess should have enough priviliges. Otherwise, there'll occur
    permission or privilige errors when the asp.net application run.

    2. Based on your situation, since the problem occurs when you set
    impersonation as true. I think it is most likey caused by the permission.
    Just the account the workprocess under which doesn't have enough permission
    to accomplish certain operation. You may have a try using different account
    to test it(use some more advanced account) to see whether the problem
    remain.

    3. As you said that the problem occurs everytime you have modified the
    web.config file and restart the process( do you mean you restart debugging
    the application via F5 in VS.NET)? If so, since the ASP.NET application
    will restart the whole application when the web.config file has been
    modified. I believe the problem caused by something called when the web
    application starts(such as at the Application_Start event). You may have a
    further check to confirm this.

    4.You also mentioned that you the problem is likely caused via the C++
    class when one of its method is first called when the web application
    starts. Then, I think maybe there occurs some permission error when the
    executing the code in that method since the ASP.NET workprocess is
    executing under the impersonated account( not the default aspnet account)
    when you use impersonate=true. So please have a look at the function in the
    c++ class. Maybe some operation in it failed so that a certain reference is
    not set to a correct value, then the "NullReferenceException" occur.

    Please try out the preceding suggestions and let me know whether thay help.



    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    MSFT, Dec 17, 2003
    #3
  4. AAguiar

    AAguiar Guest

    Thanks for your answer.

    I've been debugging the code again (in VS.NET) and checked out the users
    permission (with <authentication mode="Windows"> .. in web.config file)
    and:
    - The user that I'm using when impersonation is set to true have the
    sufficient persmissions to access all resources.
    - The problem only appears when I modify the web.config file which causes
    the aspnet worker to be recycled.
    - If I restart the aspnet worker proccess (aspnet_wp), the application
    begins to work correctly again, it works fine (the user is always the same).
    When I debug it, the debugger steps into the method correctly.
    - The NullReferenceException ocurr at the point the c# class calls the
    static c++ class, and not inside the method, it doesn't step into the method
    (I tested it calling a dummy method and the error is the same, it doesn't
    reach the inside the method), I think the Exception appears because the
    static c++ class doesn't exist, is "null", or something like that.

    It's very strange this behaviour... Any other guide or experience on this
    problem is welcomed.

    Thanks again.




    "MSFT" <> wrote in message
    news:...
    > Hi AAguiar,
    >
    > Thank you for using Microsoft Newsgroup Service. Based on your

    description,
    > you are dealing with a ASP.NET web application. It seems that you called
    > one function in a Managed C++ class when the web application start. The
    > application runs well when you set the web.config's impersonate attribute
    > as false. However, when change it as ture, you found that there occured an
    > "NullReferenceException: Object reference not set to an instance of an
    > object" error when starting the web application.( Please correct me if my
    > understanding of your issue is not quite exact.)
    >
    > If so, here is my suggestion on it:
    >
    > 1. As for the <identity impersonate="true"/> attribute in the ASP.NET
    > web.config file, if you set the authentication mode as "windows" in the
    > web.config file for asp.net application. the "impersonte" attribute will
    > determine under which account will the asp.net execute code. If you set as
    > false. I'll use the default process acount(MACHINE\ASPNET). If you set it
    > as true, the workprocess will execute under the account flow from the IIS
    > SERVER. Also, you can explicitly specify a certain account for the
    > workproecc to execute under.
    >
    > here is the description in MSDN:
    > Another important security feature is the ability to control the identity
    > under which code is executed. Impersonation is when ASP.NET executes code
    > in the context of an authenticated and authorized client. By default,
    > ASP.NET does not use impersonation and instead executes all code using the
    > same user account as the ASP.NET process, which is typically the ASPNET
    > account. This is contrary to the default behavior of ASP, which uses
    > impersonation by default. In Internet Information Services (IIS) 6, the
    > default identity is the NetworkService account.
    >
    > you can follow the link:
    >

    http://msdn.microsoft.com/library/en-us/vsent7/html/vxconimpersonation.asp?f
    > rame=true
    > to get more detailed info on impersonation in ASP.NET.
    >
    > So, if you use the "impersonate" function(set it as true in web.config).
    > You should make sure that the user accounts which will be impersonated by
    > the workprocess should have enough priviliges. Otherwise, there'll occur
    > permission or privilige errors when the asp.net application run.
    >
    > 2. Based on your situation, since the problem occurs when you set
    > impersonation as true. I think it is most likey caused by the permission.
    > Just the account the workprocess under which doesn't have enough

    permission
    > to accomplish certain operation. You may have a try using different

    account
    > to test it(use some more advanced account) to see whether the problem
    > remain.
    >
    > 3. As you said that the problem occurs everytime you have modified the
    > web.config file and restart the process( do you mean you restart debugging
    > the application via F5 in VS.NET)? If so, since the ASP.NET application
    > will restart the whole application when the web.config file has been
    > modified. I believe the problem caused by something called when the web
    > application starts(such as at the Application_Start event). You may have a
    > further check to confirm this.
    >
    > 4.You also mentioned that you the problem is likely caused via the C++
    > class when one of its method is first called when the web application
    > starts. Then, I think maybe there occurs some permission error when the
    > executing the code in that method since the ASP.NET workprocess is
    > executing under the impersonated account( not the default aspnet account)
    > when you use impersonate=true. So please have a look at the function in

    the
    > c++ class. Maybe some operation in it failed so that a certain reference

    is
    > not set to a correct value, then the "NullReferenceException" occur.
    >
    > Please try out the preceding suggestions and let me know whether thay

    help.
    >
    >
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
     
    AAguiar, Dec 17, 2003
    #4
  5. AAguiar

    MSFT Guest

    Hi AAguiar,

    Thank you for the reply. Since the problem is a bit strange. I suggestion
    that you first remove(comment) those code which call the static c++
    class(include other code concerned with the c++ class). Then, try to see
    whether the problem remain so that we can get whether the problem is
    acutually caused by something within the C++ class.

    Additionally, would you plesase set on the Application's Trace option so as
    to get some Trace stack infos about the error. Maybe it will provide some
    useful clues.

    In the mean time, I'll do some further research on it to look for some more
    clues.


    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    MSFT, Dec 18, 2003
    #5
  6. AAguiar

    MSFT Guest

    Hi AAguiar,

    Have you had a chance to try my suggestion or have your problem been
    resolved? Please let me know if you have any question on it. Thanks.

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    MSFT, Dec 22, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven Cheng[MSFT]
    Replies:
    1
    Views:
    406
    AAguiar
    Jan 9, 2004
  2. Steven Cheng[MSFT]
    Replies:
    11
    Views:
    617
    AAguiar
    Jan 23, 2004
  3. Stefan Mueller
    Replies:
    5
    Views:
    551
    Steven Saunderson
    Jul 10, 2006
  4. CSharpner
    Replies:
    0
    Views:
    1,145
    CSharpner
    Apr 9, 2007
  5. joes
    Replies:
    3
    Views:
    330
Loading...

Share This Page