Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AddInMemoryClients results in Unknown client or not enabled

I'm trying to get Identity server 4 to work in an ASP Net Core 3 application with an Angular 8 SPA using "oidc-client": "1.10.1".

If I add the following to my appsettings.json

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

Using this client:

 {
      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
  }

I get: Invalid redirect_uri: http://localhost:4200/auth-callback

adding.

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

does nothing. If I add the Client config copied (almost) from the documentation

"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
  }
]

I get: System.InvalidOperationException: 'Type '' is not supported.' at startup

If I try to configure the client in code, and only keep the "Key" section in appsettings

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"
    }
}
})

I get: Unknown client or not enabled: dev-client.

Someone help me keep my sanity and point out my, most likely obvious, error.

like image 868
JensB Avatar asked Jan 14 '20 20:01

JensB


1 Answers

ASP.NET Identity overrides the documented method for IdentityServer Clients configuration, expecting a dictionary of well-known values. You can bypass this by creating a section that is not named Clients and reading from that section explicitly. Additionally, AddApiAuthorization exposes the Clients collection on the ApiAuthorizationOptions, which can be used to add other clients:

.AddApiAuthorization<...>(options =>
            {
                options.Clients.AddRange(Configuration.GetSection("IdentityServer:OtherClients").Get<Client[]>());
            });
like image 60
James Avatar answered Nov 12 '22 11:11

James