【问题标题】:IdentityServer4 -How can I add clientId and clientSecret to services?IdentityServer4 - 如何将 clientId 和 clientSecret 添加到服务中?
【发布时间】:2022-10-13 23:38:21
【问题描述】:

我有 IdentityServer 项目和 API。我从 IdentityServer 获取 JWT 令牌以向我的 API 发送请求。

在我的 API 中,我不在任何地方使用 clientId 和 clientSecret。我不明白为什么???

在我研究的时候,API的Program.cs被很多人这样使用;

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
{
   options.Authority = "[identity server url]";
   options.Audience = "[api resourse name]";
   options.RequireHttpsMetadata = false;
});

我不明白的是,这里没有clientId和clientSecret。

有些人使用 [IdentityServer4.AccessTokenValidation] 通过使用this 来做我想做的事;

services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication(options =>
{
    options.Authority = "https://demo.identityserver.io";
    options.ApiName = "api1";
    options.ApiSecret = "secret";
});

但是当我尝试这个时,即使我的 clientSecret 不正确,我的 api 也不会返回 401 Unauthorized 并返回 200 OK。

我怎么解决这个问题 ?

【问题讨论】:

  • 您的客户端密码是否设置在 IdentityServer 的 Config.cs 文件中?我使用 OIDC 作为中间件没有任何问题。

标签: c# asp.net-core oauth-2.0 identityserver4


【解决方案1】:

您在 API 中使用的 AddJwtBearer 身份验证处理程序不需要 clientid/secret,因为它需要做的就是使用从 IdentityServer 发现文档下载的公钥接受和验证传入的访问令牌。

如果您需要同时支持 JWT 和引用令牌,我认为 IdentityServer4.AccessTokenValidation 主要有用。但通常你只需要 JWT 令牌。

【讨论】:

    猜你喜欢
    • 2021-07-21
    • 2017-09-01
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多