Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# clearing the thread principal

Tags:

c#

wpf

How do you clear the thread principal in c#.

I have a background thread that does a

Membership.ValidateUser(username, password);

which then copies the resulting Principal back to the main thread

AppDomain.CurrentDomain.SetThreadPrincipal(Thread.CurrentPrincipal);

this works fine. But, if I log off I want to clear the principal, if I set it to null it does nothing Thread.CurrentPrincipal = null; if I try and set it again via

AppDomain.CurrentDomain.SetThreadPrincipal(Thread.CurrentPrincipal);

I get the error

Default principal object cannot be set twice.

Any ideas?

like image 806
gimpy Avatar asked Mar 12 '09 05:03

gimpy


2 Answers

I don't think you can reset the principal without shutting down the AppDomain and recreating it. You only get one shot at calling SetThreadPrincipal.

Assuming that you are using your own custom principal object that you create after ValidateUser; you can probably put a "Logout" method on your principal that resets its internal state to an unauthenticated user.

like image 128
DancesWithBamboo Avatar answered Oct 16 '22 17:10

DancesWithBamboo


The trick to solve this was to create a facade principal and assign that to the thread. Then behind it you can switch out the current user for a new one.

like image 40
gimpy Avatar answered Oct 16 '22 15:10

gimpy