public void AuthenticationServiceIsInjectedImpersonationService()
        {
            MockAuthenticationService authService =
                mockContainer.Services.AddNew <MockAuthenticationService, IAuthenticationService>();
            IImpersonationService impersonator =
                mockContainer.Services.AddNew <GenericPrincipalImpersonationService, IImpersonationService>();

            Assert.AreEqual(impersonator.AuthenticationService, authService);
        }
        public void ImpersonateFailsIfAuthenticationFails()
        {
            string currentUser = Thread.CurrentPrincipal.Identity.Name;
            string userName    = "******";

            MockAuthenticationService authService =
                mockContainer.Services.AddNew <MockAuthenticationService, IAuthenticationService>();

            authService.Identity = userName;

            IImpersonationService impersonator =
                mockContainer.Services.AddNew <GenericPrincipalImpersonationService, IImpersonationService>();

            // fails
            impersonator.Impersonate();
        }
        public void ImpersonationContextCallsUndoOnDispose()
        {
            string currentUser          = Thread.CurrentPrincipal.Identity.Name;
            string impersonatedUsername = "******";

            MockAuthenticationService authService =
                mockContainer.Services.AddNew <MockAuthenticationService, IAuthenticationService>();

            authService.Identity = impersonatedUsername;

            IImpersonationService impersonator =
                mockContainer.Services.AddNew <GenericPrincipalImpersonationService, IImpersonationService>();

            using (IImpersonationContext context = impersonator.Impersonate())
            {
                Assert.IsTrue(Thread.CurrentPrincipal.Identity.IsAuthenticated);
                Assert.AreEqual(impersonatedUsername, Thread.CurrentPrincipal.Identity.Name);
            }

            Assert.IsTrue(Thread.CurrentPrincipal.Identity.IsAuthenticated);
            Assert.AreEqual(currentUser, Thread.CurrentPrincipal.Identity.Name);
        }
        public void ImpersonateChangesIdentity()
        {
            string currentUser          = Thread.CurrentPrincipal.Identity.Name;
            string impersonatedUsername = "******";

            MockAuthenticationService authService =
                mockContainer.Services.AddNew <MockAuthenticationService, IAuthenticationService>();

            authService.Identity = impersonatedUsername;

            IImpersonationService impersonator =
                mockContainer.Services.AddNew <GenericPrincipalImpersonationService, IImpersonationService>();
            IImpersonationContext context = impersonator.Impersonate();

            // user was impersonated
            Assert.IsTrue(Thread.CurrentPrincipal.Identity.IsAuthenticated);
            Assert.AreEqual(impersonatedUsername, Thread.CurrentPrincipal.Identity.Name);

            context.Undo();

            // previous principal
            Assert.IsTrue(Thread.CurrentPrincipal.Identity.IsAuthenticated);
            Assert.AreEqual(currentUser, Thread.CurrentPrincipal.Identity.Name);
        }