【问题标题】:AddInMemoryClients results in Unknown client or not enabledAddInMemoryClients 导致未知客户端或未启用
【发布时间】:2020-05-01 14:31:58
【问题描述】:

我正在尝试使用 "oidc-client": "1.10.1" 让 Identity server 4 在带有 Angular 8 SPA 的 ASP Net Core 3 应用程序中工作。

如果我将以下内容添加到我的 appsettings.json

  "IdentityServer": {
    "Key": {
      "Type": "File",
      "FilePath": "acertificate.pfx",
      "Password": "notmyrealpassword..orisit?"
    },
    "Clients": {
      "dev-client": {
        "Profile": "IdentityServerSPA",
      }
    }
  }

使用此客户端:

 {
      authority: 'https://localhost:5001/',
      client_id: 'dev-client',
      redirect_uri: 'http://localhost:4200/auth-callback',
      post_logout_redirect_uri: 'http://localhost:4200/',
      response_type: 'id_token token',
      scope: 'openid profile API',
      filterProtocolClaims: true,
      loadUserInfo: true
  }

我得到:Invalid redirect_uri: http://localhost:4200/auth-callback

添加。

"dev-client": {
  "Profile": "IdentityServerSPA",
  "RedirectUris": [ "http://localhost:4200/auth-callback" ]
}

什么都不做。如果我添加(几乎)从文档中复制的客户端配置

"Clients": [
  {
    "Enabled": true,
    "ClientId": "dev-client",
    "ClientName": "Local Development",
    "AllowedGrantTypes": [ "implicit" ],
    "AllowedScopes": [ "openid", "profile", "API" ],
    "RedirectUris": [ "http://localhost:4200/auth-callback" ],
    "RequireConsent": false,
    "RequireClientSecret": false
  }
]

我在启动时得到:System.InvalidOperationException: 'Type '' is not supported.'

如果我尝试在代码中配置客户端,并且只保留 appsettings 中的“Key”部分

services
.AddIdentityServer(options =>
{
    options.Cors.CorsPolicyName = _CorsPolicyName;
})
.AddInMemoryClients(new IdentityServer4.Models.Client[] {
new IdentityServer4.Models.Client
{
    ClientId = "dev-client",
    ClientName = "JavaScript Client",
    ClientUri = "http://localhost:4200",

    AllowedGrantTypes = { IdentityModel.OidcConstants.GrantTypes.Implicit },
    AllowAccessTokensViaBrowser = true,

    RedirectUris = { "http://localhost:4200/auth-callback" },
    PostLogoutRedirectUris = { "http://localhost:4200" },
    AllowedCorsOrigins = { "http://localhost:4200" },

    AllowedScopes =
    {
        IdentityServer4.IdentityServerConstants.StandardScopes.OpenId,
        IdentityServer4.IdentityServerConstants.StandardScopes.Profile,
        IdentityServer4.IdentityServerConstants.StandardScopes.Email,
        "API"
    }
}
})

我得到:Unknown client or not enabled: dev-client

请有人帮助我保持理智,并指出我最明显的错误。

【问题讨论】:

  • .net core 3 的文档仍然很松散......我遇到了类似的问题
  • 有什么解决办法吗?
  • @Tany3450 不。我现在使用的是另一个安全提供商,而不是 Identity Server,我的生活因此变得更好。
  • 老实说,在 asp net core 身份服务器集成中发生了太多“魔法”,我无法在生产中轻松使用它。一个小错误,整个事情就炸了,你真的无法在任何地方得到帮助。

标签: asp.net-core .net-core identityserver4 angular8


【解决方案1】:

ASP.NET Identity 覆盖了 IdentityServer 客户端配置的文档化方法,需要 dictionary of well-known values。您可以通过创建一个名为Clients 的部分并明确读取该部分来绕过此问题。此外,AddApiAuthorization 在 ApiAuthorizationOptions 上公开了 Clients 集合,可用于添加其他客户端:

.AddApiAuthorization<...>(options =>
            {
                options.Clients.AddRange(Configuration.GetSection("IdentityServer:OtherClients").Get<Client[]>());
            });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-25
    • 2021-12-22
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 2020-02-27
    • 1970-01-01
    相关资源
    最近更新 更多