Upgraded to Keycloak 16.0 - Impersonation NOT working though KEYCLOAK_IDENTITY and KEYCLOAK_SESSION Created in Cookie

Please help me on this,

Upgraded to Keycloak 16.0 - Impersonation NOT working though KEYCLOAK_IDENTITY and KEYCLOAK_SESSION Created in Cookie

We have upgraded the Keycloak from 3.x to 16.x - when we try to do an impersonation via REST API https://{BASE_URL}/auth/admin/realms/{REALM_NAME}/users/{USER_ID}/impersonation

This endpoint is creating all required Usersession (UserSessionModel) and creating an LoginCookie createLoginCookie(AuthenticationManager) After redirection, the Keycloak couldn’t able to create an impersonated login session.

Please help me with this. let me know if you need more details.

/**
 * Impersonate the user
 *
 * @return
 */
@Path("impersonation")
@POST
@NoCache
@Produces(MediaType.APPLICATION_JSON)
public Map<String, Object> impersonate() {
    ProfileHelper.requireFeature(Profile.Feature.IMPERSONATION);

    auth.users().requireImpersonate(user);
    RealmModel authenticatedRealm = auth.adminAuth().getRealm();
    // if same realm logout before impersonation
    boolean sameRealm = false;
    String sessionState = auth.adminAuth().getToken().getSessionState();
    if (authenticatedRealm.getId().equals(realm.getId()) && sessionState != null) {
        sameRealm = true;
        UserSessionModel userSession = session.sessions().getUserSession(authenticatedRealm, sessionState);
        AuthenticationManager.expireIdentityCookie(realm, session.getContext().getUri(), clientConnection);
        AuthenticationManager.expireRememberMeCookie(realm, session.getContext().getUri(), clientConnection);
        AuthenticationManager.backchannelLogout(session, authenticatedRealm, userSession, session.getContext().getUri(), clientConnection, headers, true);
    }
    EventBuilder event = new EventBuilder(realm, session, clientConnection);

    UserSessionModel userSession = session.sessions().createUserSession(realm, user, user.getUsername(), clientConnection.getRemoteAddr(), "impersonate", false, null, null);

    UserModel adminUser = auth.adminAuth().getUser();
    String impersonatorId = adminUser.getId();
    String impersonator = adminUser.getUsername();
    userSession.setNote(IMPERSONATOR_ID.toString(), impersonatorId);
    userSession.setNote(IMPERSONATOR_USERNAME.toString(), impersonator);

    AuthenticationManager.createLoginCookie(session, realm, userSession.getUser(), userSession, session.getContext().getUri(), clientConnection);
    URI redirect = AccountFormService.accountServiceBaseUrl(session.getContext().getUri()).build(realm.getName());
    Map<String, Object> result = new HashMap<>();
    result.put("sameRealm", sameRealm);
    result.put("redirect", redirect.toString());
    event.event(EventType.IMPERSONATE)
            .session(userSession)
            .user(user)
            .detail(Details.IMPERSONATOR_REALM, authenticatedRealm.getName())
            .detail(Details.IMPERSONATOR, impersonator).success();

    return result;
}