Idenity Server 4在IAuthorizeInteractionResponseGenerator中注入其他声明

Dan*_*Dan 5 openid openid-connect identityserver4

我正在尝试从IdentityServer3移植代码,该代码使用PreAuthenticate为我们的管理员提供用户临时模拟客户端应用程序.

接下来是这里的线程,我在自定义IAuthorizeInteractionResponseGenerator中重写ProcessInteractionAsync.这很好用,我有一切正常,但是,我无法添加额外的声明被发送回客户端应用程序,以便它知道这是一个模仿的id_token.在我的覆盖中替换ValidatedAuthorizeRequest上的主题时,我添加了一个额外的声明,指定启动模拟的用户,但此声明未在id_token或访问令牌中贯彻执行.这是我的覆盖:

public override async Task<InteractionResponse> ProcessInteractionAsync(ValidatedAuthorizeRequest request, ConsentResponse consent = null)
{
    string impersonatedUserName = request.GetPrefixedAcrValue("Impersonate:");
    if (!string.IsNullOrWhiteSpace(impersonatedUserName))
    {
        if (request.Client.AllowedScopes.Contains(Constants.ClaimTypes.Impersonation))
        {
            var currentUser;
            var impersonatedUser;

            //Omited code to verify eligibility to impersonate user

            if (impersonatedUser != null)
            {
                IEnumerable<string> requestedClaimTypes = request.Client.AllowedScopes;

                IdentityServerUser idSrvUser = new IdentityServerUser(impersonatedUser.Id.ToString())
                {
                    AuthenticationTime = Clock.UtcNow.UtcDateTime,
                    DisplayName = impersonatedUser.UserName,
                    IdentityProvider = !string.IsNullOrEmpty(impersonatedUser.PasswordHash) ? IdentityServerConstants.LocalIdentityProvider : "external"
                };

                ProfileDataRequestContext context = new ProfileDataRequestContext(
                    idSrvUser.CreatePrincipal(),
                    request.Client,
                    nameof(AuthorizeInteractionResponseGenerator),
                    requestedClaimTypes);

                await Profile.GetProfileDataAsync(context);

                //Need this claim to flow through to client
                context.IssuedClaims.Add(new Claim(Constants.ClaimTypes.Impersonation, currentUser.UserName));

                foreach (Claim claim in context.IssuedClaims)
                {
                    idSrvUser.AdditionalClaims.Add(claim);
                }

                ClaimsPrincipal newSubject = idSrvUser.CreatePrincipal();

                request.Subject = newSubject;

                Logger.LogInformation("Impersonation set, returning response");

                return new InteractionResponse();
            }
            else
            {
                Logger.LogWarning("Invalid attempt to impersonate user");
                return new InteractionResponse { Error = "Invalid attempt to impersonate user" };
            }
        }
        else
        {
            Logger.LogWarning("Client does not support impersonation!");
            return new InteractionResponse { Error = "Client does not support impersonation" };
        }
    }

    return await base.ProcessInteractionAsync(request, consent);
}
Run Code Online (Sandbox Code Playgroud)

我为客户请求的这个特殊声明添加了一个范围,但它仍然没有被包括在内.我觉得这里有一些显而易见的东西,我如何在其中一个代币中添加额外的声明?或者是否有更好的方式来表示开始模仿的客户?

Dan*_*Dan 0

因此,我最终采取了与 Identity Server 3 中略有不同的路线来解决此需求。我没有将用户名附加到 Identity Server 中,而是将在客户端启动模拟的用户的用户名存储在临时 cookie 中。然后,在 AuthorizationCodeReceived 通知中,我检查 cookie 值并将声明附加到在那里创建的主体。

不像在 Identity 中附加声明那么万无一失,但由于所有主要的安全检查仍然会发生,并且我想知道谁作为模拟用户执行此操作,如果没有 cookie,就会失败,所以这会起作用。如果其他人遇到此问题并且确实弄清楚如何在身份服务器中附加声明,请回复,我会给你答案