【问题标题】:How to add custom claim on Jwt Token based on user input?如何根据用户输入在 Jwt Token 上添加自定义声明?
【发布时间】:2021-09-24 21:57:42
【问题描述】:

我在将自定义声明(租户 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
        });

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

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

【问题讨论】:

    标签: c# asp.net .net jwt identityserver4


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-07-20
      • 2019-02-26
      • 2018-07-05
      • 1970-01-01
      • 2017-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-04
      相关资源
      最近更新 更多