Esempio n. 1
0
        public void NoImpersonation()
        {
            var testUser = new FakeUserInfo("TestUser");

            var impersonationService = ImpersonationServiceHelper.CreateImpersonationService(testUser).ImpersonationService;

            var user = impersonationService.GetAuthenticationInfo();

            Assert.AreEqual(
                "No impersonation, original TestUser",
                ReportImpersonationStatus(user));
        }
Esempio n. 2
0
        public void SimpleImpersonation()
        {
            var testUser            = new FakeUserInfo("TestUser");
            var impersonateUserName = "******";
            var cookie = ImpersonationServiceHelper.SetImpersonation(testUser, impersonateUserName);

            (var impersonationService, var httpContext, _) = ImpersonationServiceHelper.CreateImpersonationService(testUser);
            httpContext.RequestCookies.Add(cookie);
            var user = impersonationService.GetAuthenticationInfo();

            Assert.AreEqual(
                "TestUser as TestImpersonatedUser, original TestUser",
                ReportImpersonationStatus(user));
        }
Esempio n. 3
0
        public void RenewCookieAfterHalfExpirationTime_HalfTimeHasPassed()
        {
            var testUser            = new FakeUserInfo("TestUser");
            var impersonateUserName = "******";

            var options = new ImpersonationOptions {
                CookieDurationMinutes = 3
            };

            var cookie            = ImpersonationServiceHelper.SetImpersonation(testUser, impersonateUserName, options);
            var impersonationInfo = ImpersonationServiceHelper.DecryptCookieValue(cookie.Value);

            AssertIsWithinOneSecond(
                DateTime.Now.AddMinutes(options.CookieDurationMinutes),
                impersonationInfo.Expires); // Reviewing the test setup.

            // Half-time has passed:

            impersonationInfo.Expires = DateTime.Now.AddMinutes(options.CookieDurationMinutes / 2.0).AddSeconds(-1);
            cookie.Value = ImpersonationServiceHelper.EncryptCookieValue(impersonationInfo);

            (var impersonationService, var httpContext, _) = ImpersonationServiceHelper.CreateImpersonationService(testUser, options);
            httpContext.RequestCookies.Add(cookie);

            var user = impersonationService.GetAuthenticationInfo();

            // Impersonation should still be valid, but the cookie expiration time is extended:

            Assert.AreEqual(
                "TestUser as TestImpersonatedUser, original TestUser",
                ReportImpersonationStatus(user));

            var returnedCookie = httpContext.ResponseCookies.Single();

            Assert.AreEqual(ImpersonationService.CookieKey, returnedCookie.Key);
            AssertIsWithinOneSecond(
                DateTime.Now.AddMinutes(options.CookieDurationMinutes),
                ImpersonationServiceHelper.DecryptCookieValue(httpContext.ResponseCookies.Single().Value).Expires);
        }