I have a library code that can be called from several types of clients, such as WinForms, Console, ASP.NET, etc ... and which should determine the current principal. In doing so, I perform a two-step verification of Thread.CurrentPrincipal and then Environment.UserName as follows:
var currentUser = !System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated ? null : System.Threading.Thread.CurrentPrincipal.Identity.Name; if (string.IsNullOrWhiteSpace(currentUser)) { currentUser = Environment.UserName; }
In the console application Thread.CurrentPrincipal.Identity.IsAuthenticated is always a false howerver in MSTest, it always has a valid authenticated user.
Is there a Thread.CurrentPrincipal value in the unit test for reset for unauthorized to simulate a console application?
source share