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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With