【问题标题】:how to get the client_id that genereted a bearer token? (.NetCore2.1, IdentityServer4)如何获取生成不记名令牌的client_id? (.NetCore2.1,IdentityServer4)
【发布时间】:2018-11-10 14:36:55
【问题描述】:

我使用 netcore 2.1 和 identityserver4 以及资源所有者密码流程

我需要在一个请求中获取生成令牌持有者的 client_id 存在获取client_id的一种方法吗? 数据库 userId,token,client_id 中是否存在关系? 问题是我不知道什么 client_id 发出请求

【问题讨论】:

  • 你为什么想要/需要那个?您可以配置客户端以添加其他声明(我不会将 clientID 放在那里,而是将 client : MyMobileAppName 之类的东西添加到身份令牌中(可能需要调整 IProfileService 以确保将声明添加到令牌...
  • 好的,¿你能告诉我怎么做吗?请举个例子,我是.net的新手
  • 我在 net core、多个数据库和多个客户端中有一个 API,在 client_id 的函数中我获取数据库的信息。

标签: asp.net-core identityserver4


【解决方案1】:

我在 net core 中有一个 API,多个数据库和多个客户端,在 client_id 的函数中我获取一个数据库的信息

默认情况下,身份服务器 4 发出的访问令牌包含 client_id 声明:

客户端使用访问令牌向您的 web api 发送请求后,在 web api 端,将身份验证服务添加到 DI 并将身份验证中间件添加到管道:

1.将 IdentityServer4.AccessTokenValidation NuGet 包添加到您的项目中

2. 将启动更新为如下所示:

 public void ConfigureServices(IServiceCollection services)
{
    services.AddMvcCore()
        .AddAuthorization()
        .AddJsonFormatters();

    services.AddAuthentication("Bearer")
        .AddIdentityServerAuthentication(options =>
        {
            options.Authority = "http://localhost:5000";
            options.RequireHttpsMetadata = false;

            options.ApiName = "api1";
        });
}

public void Configure(IApplicationBuilder app)
{
    app.UseAuthentication();

    app.UseMvc();
}

然后您可以获得包含客户 ID 的声明:

【讨论】:

    猜你喜欢
    • 2018-06-21
    • 2021-07-12
    • 2022-01-19
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    • 2019-08-04
    相关资源
    最近更新 更多