public IActionResult StartEnhanced(string userId, string userName, [FromServices] IImpersonationService service) { var errorMessage = service.StartImpersonation(userId, userName, true); return(RedirectToAction(nameof(Message), new { errorMessage, successMessage = $"You are now impersonating user {userName} with your own permissions." })); }
[Authorize] //you must be logged in //Note: anyone call call Stop, as when impersonating someone you don't know what permissions (if any) that they have public IActionResult Stop([FromServices] IImpersonationService service) { var errorMessage = service.StopImpersonation(); return(RedirectToAction(nameof(Message), new { errorMessage, successMessage = $"You have stopped impersonating another user." })); }
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 AuthenticationServiceIsInjectedImpersonationService() { MockAuthenticationService authService = mockContainer.Services.AddNew <MockAuthenticationService, IAuthenticationService>(); IImpersonationService impersonator = mockContainer.Services.AddNew <GenericPrincipalImpersonationService, IImpersonationService>(); Assert.AreEqual(impersonator.AuthenticationService, authService); }
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 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); }
public MyService(IEventHandler eventHandler, IImpersonationService impersonationService) { _eventHandler = eventHandler; _impersonationService = impersonationService; }