Access denied while start threading with impersonation enabled.

Last post 01-24-2007 1:32 AM by Leijun Jie - MSFT. 1 replies.

Sort Posts:

  • Access denied while start threading with impersonation enabled.

    01-16-2007, 9:49 AM
    • Member
      83 point Member
    • suicidesquad
    • Member since 11-24-2005, 8:31 AM
    • Netherlands
    • Posts 58

    Hello, 

    In my c# website I have set the <identity impersonate="true" /> in the web.config.

    I am trying to start a thread in my web service in the following manner:

                if (!isThreadRunning)
                {
                    ThreadStart startThread = new ThreadStart(InvokeKeepAliveThread);
                    Thread keepAliveThread = new Thread(startThread);
                    keepAliveThread.IsBackground = bool.Parse("true");
                    keepAliveThread.Priority = ThreadPriority.Lowest;
                    keepAliveThread.Start();
                }          
            }
            
            public void InvokeKeepAliveThread()
            {
                while (true)
                {
                    CheckExpiredLicenses();
                    Thread.Sleep(300000);    // Check once in 5 minutes
                }
            }

     Somehow on my computer it is working fine, but on the computer of my collegue he gets the error:

    System.ComponentModel.Win32Exception: Access is denied
    When I set impersonation to false in the web.config this part works, but I need to use impersonation somewhere else.

    Therefor I was looking for a solution to disable impersonation temporary, to start the thread and then enable it again to use it in another part of the project.

    Is there a way to disable impersonation for a while, or is there another solution?

    Thanks in advance!

     

     

  • Re: Access denied while start threading with impersonation enabled.

    01-24-2007, 1:32 AM
    Answer
    Sure you can set impersonate to false in web.config, and in some code temporarily set impersonation to true to perform some task. Check the "Impersonate the Authenticating User in Code" section in this KB:
    How to implement impersonation in an ASP.NET application
    The future is now...
    Sincerely,
    LeiJun Jie
    Microsoft Online Community Support
Page 1 of 1 (2 items)