/// <summary> /// Impersonates the specified user by the current admin user. /// </summary> /// <param name="originalUrl">If not specified, the current HTTP request's URL will be used.</param> public static async Task Impersonate(ILoginInfo user, bool redirectToHome = true, string originalUrl = null) { if (user == null) { throw new ArgumentNullException(nameof(user)); } var admin = GetCurrentUser() as IImpersonator ?? throw new InvalidOperationException("The current user is not an IImpersonator."); if (!admin.CanImpersonate(user)) { throw new InvalidOperationException("The current user is not allowed to impersonate the specified user."); } var token = Guid.NewGuid().ToString(); await Entity.Database.Update(admin, o => o.ImpersonationToken = token); SetImpersonationToken(token); SetOriginalUrl(originalUrl.Or(Context.Request.ToRawUrl())); await user.LogOn(); if (redirectToHome && !Context.Request.IsAjaxCall()) { Context.Response.Redirect("~/"); } }