public TOutput WithImpersonation <TOutput>(string authToken, string useridToImpersonate, Func <TOutput> action) { ImpersonatedUserGuid.Clear(); var authUser = _userService.GetByAuthenticationToken(authToken); if (authUser == null || !authUser.CanImpersonate) { throw (new ImpersonationNotAllowedException()); } var user = _userService.GetByUserId(useridToImpersonate); if (user == null) { throw (new ImpersonationUserNotFoundException(useridToImpersonate)); } ImpersonatedUserGuid.Set(user.Guid, authToken); try { return(action()); } finally { ImpersonatedUserGuid.Clear(); } }
public void TestImpersonatedUserGuid() { Assert.IsFalse(ImpersonatedUserGuid.HasValue()); ImpersonatedUserGuid.Set("123", "abcdef"); Assert.IsTrue(ImpersonatedUserGuid.HasValue()); Assert.AreEqual("123", ImpersonatedUserGuid.Get()); ImpersonatedUserGuid.Set(" ", " "); Assert.IsFalse(ImpersonatedUserGuid.HasValue()); ImpersonatedUserGuid.Set("123", "abcdef"); ImpersonatedUserGuid.Clear(); Assert.IsFalse(ImpersonatedUserGuid.HasValue()); }