Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jack Burdis 3 posts 83 karma points
    Nov 07, 2022 @ 14:56
    Jack Burdis
    0

    Umbraco 10 - Member Impersonation

    Hello,

    I'm trying to impersonate a member and login as them via a click on a "Login as Member" button in the backoffice that redirects to the frontend.

    I found this line of code below but can't seem to find the equivalent in Umbraco 10 :

    FormsAuthentication.SetAuthCookie(_response.Member.Email, false);
    

    I tried to implement the code below to no avail :

    var _principal = CreatePrincipal(_response);
    
            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, _principal);
    

    Has anyone had any luck with a similar situation? Some help on this would be much appreciated.

    Cheers.

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Nov 10, 2022 @ 16:18
    Huw Reddick
    101

    This is what I do, I have a list control which lists the members in the front end if the logged in member is an Administrator, they can select a member to impersonate.

        public IActionResult Impersonate([FromQuery]string memberId)
        {
            //Get the member to impersonate
            var member = _memberService.GetById(Convert.ToInt32(memberId));
            var signinuser = _memberManager.FindByNameAsync(member.Username).Result;
    
            //Get the currently logged in
            var currentUser = _memberManager.GetCurrentMemberAsync().Result;
            var currMember = _memberService.GetByKey(currentUser.Key);
    
            //signout currentuser
            _signInManager.SignOutAsync();
    
            //Sign in as impersonated user
            _signInManager.SignInAsync(signinuser,false);
            HttpContext.Session.SetString("portalImpersonate", currentUser.Id);
    
            return CurrentUmbracoPage();
    
        }
    
  • Jack Burdis 3 posts 83 karma points
    Nov 17, 2022 @ 09:56
    Jack Burdis
    0

    Hi Huw,

    Implemented your solution yesterday and it worked perfectly. Really appreciate your help on this one. Have a great rest of the week.

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Nov 17, 2022 @ 10:17
    Huw Reddick
    0

    Glad it helped.

    I also added a session variable which is set to currentUser.Id to indicate the user was impersonating someone, then added code to my logout method which checks the session variable and if set, logs you back in as the previous user.

Please Sign in or register to post replies

Write your reply to:

Draft