none
BLAZOR and impersonate issue RRS feed

  • Question

  • Hi all,

      I need to set up the "impersonate" features in a BLAZOR server-side app.

    Authentication is based on standard (aspnetusers, aspnetroles table...) 

    Right now everything is good and works fine (authorized view, roles, user and so on) ...

    CRM Support needs impersonate so I'm trying to make it available... I've found a lot of things but nothings works...

    I'm sure I've forgotten something on the way... 

     Here is my code in the .razor component:

    .....

    @inject AuthenticationStateProvider AuthenticationStateProvider
    @inject NavigationManager navigationManager
    @inject UserManager<IdentityUser> userManager
    @inject SignInManager<IdentityUser> signInManager

    @using Microsoft.AspNetCore.Http
    @inject IHttpContextAccessor httpContextAccessor

    ....

    @code{....

            var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); //get the current state 

            var userPrincipal = authState.User;  //current user logged   --->  (usually ADMIN)

            //myCompany is the class choosen by the user to impersonate.

            Guid myIDUser = await companyService.GetCompanyUserIdByIdCompany(myCompany.Id.ToString());  //retrieve the user related the company choosen

            var newUser = await userManager.FindByIdAsync(myIDUser.ToString());  //get userid from ASPNETUSER table
            ClaimsPrincipal impersonate = await signInManager.CreateUserPrincipalAsync(newUser); //create new Principal

            var s = new AuthenticationState(impersonate);   //nothing happens .... 
            httpContextAccessor.HttpContext.User = impersonate; 

           // here is where I'm goin' crazy ... this change the user to the context but if I close the page the authState.User is always the old logged one (real ... the ADMIN) ... I need to set it up the impersonate user.

    Well, could you give me any suggestions to make this works?

    in smart way:

        I need to set che currentPrincipal to a custom user whenever I need in the App.

    If AuthenticationStateProvider doesn't change authorizedview doesn't works...

    TIA for any info about it.

    Dario

      


    • Edited by Dario Galvani Thursday, February 20, 2020 9:28 AM errata corrige
    Thursday, February 20, 2020 9:23 AM

Answers

  • If you mean Razor, then Razor can be discussed at the ASP.NET forums.

    http://forums.asp.net/

    • Marked as answer by Dario Galvani Thursday, February 20, 2020 3:25 PM
    Thursday, February 20, 2020 2:40 PM