AddInMemoryClients 导致未知客户端或未启用

Jen*_*nsB 5 .net-core asp.net-core identityserver4 angular8

我试图让身份服务器 4 在 ASP Net Core 3 应用程序中使用"oidc-client": "1.10.1".

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

  "IdentityServer": {
    "Key": {
      "Type": "File",
      "FilePath": "acertificate.pfx",
      "Password": "notmyrealpassword..orisit?"
    },
    "Clients": {
      "dev-client": {
        "Profile": "IdentityServerSPA",
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

使用这个客户端:

 {
      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
  }
Run Code Online (Sandbox Code Playgroud)

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

添加。

"dev-client": {
  "Profile": "IdentityServerSPA",
  "RedirectUris": [ "http://localhost:4200/auth-callback" ]
}
Run Code Online (Sandbox Code Playgroud)

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

"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
  }
]
Run Code Online (Sandbox Code Playgroud)

我得到: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"
    }
}
})
Run Code Online (Sandbox Code Playgroud)

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

有人帮助我保持理智并指出我的错误,这很可能是显而易见的错误。

小智 6

ASP.NET Identity 重写了 IdentityServer 客户端配置的记录方法,需要一个已知值的字典。您可以通过创建一个命名的部分Clients并显式读取该部分来绕过此问题。此外,AddApiAuthorization在 ApiAuthorizationOptions 上公开 Clients 集合,可用于添加其他客户端:

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