2

We have a Windows Application with a worker-thread who needs to update GUI. We uses BeginInvoke to do it asynchronously. Our problem, demonstrated in the example below, is that the Principal is propagated to GUI-thread wich we want to avoid, couse in our real application we do server requests who fails because of wrong identity. It thera a way to avoid this propagation?

  private Thread _thread;

  public Form1()
  {
     InitializeComponent();

     Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("MainUser"), new[] { "User" });

     _thread = new Thread(ThreadProc);
     _thread.Start(this);
  }

  private void ThreadProc(object parameter)
  {
     Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("ThreadUser"), new[] { "User" });

     var form = parameter as Form1;

     while (true)
     {
        form.BeginInvoke(new Action(() => ShowIdentity()));
        Thread.Sleep(4000);
     }
  }

  private void ShowIdentity()
  {
     lblIdentity.Text = Thread.CurrentPrincipal.Identity.Name;
  }
Tylland
  • 97
  • 5

2 Answers2

0

See http://blogs.msdn.com/b/shawnfa/archive/2005/03/22/400749.aspx for instructions for impersonating a Windows user in .NET. That said, if you're doing this on a client machine, the target account's password will need to be accessible under the UI user's account, which is probably something that you do not want. To prevent this, you would need to either make the server-side service accessible to the UI users or use an intermediate service (e.g.: server-hosted or Windows service on client machines) to broker calls to the server-side service while running under an account that has access to the target service.

Nicole Calinoiu
  • 20,843
  • 2
  • 44
  • 49
  • If I getting this right, this solves the problem with wrong user calling the server but not the question how to prevent worker thread from setting principal on GUI thread. But it is a still a solution, thanks. – Tylland May 21 '15 at 06:55
0

So I imagine that you get the name "MainUser" printed in the label?

What is happening is not that the Thread.Principal principal of the main thread is propagating to the other thread, but rather ShowIdentity is being done on the same thread (the main thread)!

The Invoke and BeginInvoke queue up work to be done on the thread which created the control (or as MSDN puts it "Executes the specified delegate asynchronously on the thread that the control's underlying handle was created on").

The difference between the two methods are that Invoke will block until the work-item has been completed thread whereas BeginInvoke only adds the work-item to the queue and returns immediately.

So in your example, you add an Action to this queue every 4 seconds from the thread you create. Because the UI thread is not busy doing other stuff, it will process this work very soon after it has been queued - but it will run on the UI thread (with its associated principal named "MainUser").

Does that make sense?

Chris Fewtrell
  • 7,555
  • 8
  • 45
  • 63