Login (Impersonate) As User
It is often useful to see the application as another user sees it, e.g. to help a user with a problem, or to check what the permissions of a role allow. StartSharp lets an administrator sign in as another user in the same browser, without signing out of the own account.
Since StartSharp 10.9.0. The earlier versions opened an impersonation link in a new, incognito window, which signed the administrator out otherwise, and recorded everything done there as the impersonated user. Those links (
~/Account/ImpersonateAs) no longer work.
Signing In as a User
On the User Management page, the menu of a user has Impersonate, for the users that the administrator may impersonate. The administrator confirms the own password, and the application opens as the user: the menus, pages and permissions are those of the impersonated user.
A banner at the bottom of every page tells it:
You are signed in as Nancy Davolio, impersonated by Admin. Until 14:30. Return to my account
- Return to my account ends the impersonation and goes back to the page where it began.
- The close button hides the banner until the next page, e.g. when it covers something.
The impersonation also ends:
- after
Impersonation:TimeLimitMinutes(60 by default), - when the administrator signs out, or the session of the administrator ends, e.g. by a sign-out everywhere,
- at the next request, when
Impersonation:Enabledis turned off.
Who Can Impersonate Whom
The IImpersonationPolicys decide it: a denial of any of them wins, else at least one must allow it, so nobody can
impersonate unless a policy says so. The default policy:
- lets the holders of
Administration:Impersonateimpersonate, which only a super admin can grant (it is inPermissionGrants:SuperAdminOnly), - only the users whose permissions they could grant, so nobody gets rights that they don't have,
- and never a super admin.
An application adds rules of its own as policies, e.g. a teacher who impersonates the own students, without the permission:
public class TeacherImpersonationPolicy(IPermissionService permissions, ISqlConnections connections) : IImpersonationPolicy
{
public ImpersonationDecision Check(ClaimsPrincipal impersonator, IUserDefinition target, out string? reason)
{
reason = null;
if (!permissions.HasPermission("School:ImpersonateStudents"))
return ImpersonationDecision.Abstain;
return IsStudentOf(impersonator.GetIdentifier(), target.Id) ? ImpersonationDecision.Allow : ImpersonationDecision.Abstain;
}
}
services.TryAddEnumerable(ServiceDescriptor.Singleton<IImpersonationPolicy, TeacherImpersonationPolicy>());
A page of its own asks ~/Account/CanImpersonate whether to show its button, and posts to ~/Account/Impersonate.
While Impersonating
- The security of the account can't be changed: the password, the two-factor authentication, the external logins and
the sessions of the impersonated user, and the actions that ask for the password, are refused with the
ImpersonationRestrictedcode. The My account page tells it instead of showing their links. - The audit records the impersonator:
InsertUserId,UpdateUserId, the capture log, the data audit log and the activity useClaimsPrincipal.GetAuditIdentifier(), which returns the impersonator, while the permissions stay those of the impersonated user. The exception log keeps the impersonated user, with the impersonator in the details of the event. In 10.9.0, a soft delete still writes the impersonated user toDeleteUserIdorUpdateUserId, which the next version fixes. - The activity of the user shows when the impersonation began and ended, and the impersonation has a session of its own, which the sessions of the user list.
Settings
"Impersonation": {
"Enabled": true,
"TimeLimitMinutes": 60
}
"Enabled": false turns it off: nobody can impersonate, and the ones who do return to their own accounts at their next
request.
How It Works
services.AddImpersonation() registers the impersonation manager and the default policy of Serenity.Pro.Extensions, and
the template has a page with the Account/[action] route that derives from ImpersonationPageBase, whose
CanImpersonate, Impersonate and EndImpersonation actions do the work:
[Route("Account/[action]")]
public class AccountImpersonationPage : ImpersonationPageBase
{
}
The impersonated user is signed in with a cookie whose identity has the impersonator as its Actor, and
ClaimsPrincipal.GetImpersonator() returns it, e.g. for the banner. See
Impersonation in the Same Browser for the
abstractions, and the 10.9.0 release notes to
upgrade an application from the impersonation links.