如何根据用户输入在 Jwt 令牌上添加自定义声明?

How to add custom claim on Jwt Token based on user input?

我在将自定义声明(租户 ID)添加到我的令牌时遇到问题。所以我的程序允许用户使用租户 ID 切换他们的工作区。为此,我需要接收选定的租户 ID 并在令牌中生成带有选定租户 ID 的新令牌。

为了在登录时第一次生成令牌,我使用了这段代码

var tokenResponse = await httpclient.RequestPasswordTokenAsync(new PasswordTokenRequest
                {
                    Address = discoveryDocument.TokenEndpoint,
                    UserName = model.PhoneNumber,
                    Password = model.Password,
                    ClientId = Configuration["ClientInformation:ClientId"],
                    ClientSecret = Configuration["ClientInformation:ClientSecret"]
                });

为了在他们登录切换租户后生成新令牌,我使用了刷新令牌。

 var tokenResponse = await httpclient.RequestRefreshTokenAsync(new RefreshTokenRequest
        {
            Address = discoveryDocument.TokenEndpoint,
            ClientId = Configuration["ClientInformation:ClientId"],
            ClientSecret = Configuration["ClientInformation:ClientSecret"],
            RefreshToken = model.RefreshToken
        });

我已经看到配置文件服务如何使用 claimsprincipal 接收数据,这就是我对如何在 claimsprincipal 中插入新值感到困惑的地方

所以我的问题是如何将选定的租户 ID 发送到配置文件服务?

我可以通过将用户想要的租户 ID 保存到数据库中来做到这一点,并且我可以使用配置文件服务获取租户值