public void ImpersonationServiceSupportsWindowsPrincipalImpersonation() { WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); Thread.CurrentPrincipal = principal; string currentUser = Thread.CurrentPrincipal.Identity.Name; string impersonatedUsername = "******"; WindowsAuthenticationService authService = mockContainer.Services.AddNew <WindowsAuthenticationService, IAuthenticationService>(); authService.Identity = impersonatedUsername; IImpersonationService impersonator = mockContainer.Services.AddNew <WindowsImpersonationService, 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 ApproveAndPurchaseCD(Account source, decimal amount, int duration) { try { IImpersonationService impersonator = WorkItem.Services.Get <IImpersonationService>(); using (IImpersonationContext context = impersonator.Impersonate()) { PurchaseCD(source, amount, duration); } } catch (AuthenticationException) { View.ShowMessage(Resources.UserNotAuthorizedMessage); } }
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); }