BLAZOR and impersonate issue

  • Thread starter Thread starter Dario Galvani
  • Start date Start date
D

Dario Galvani

Guest
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

Continue reading...
 
Back
Top