Asp.net-Identity

在客戶端應用程序中使用 OpenIdConnect 的 Invalid_client

  • March 10, 2022

我有一個使用 ASP.NET Identity 執行的 IdentityServer4 應用程序。我想使用它,以便來自另一個應用程序的使用者可以通過我的遠端身份伺服器登錄。

我在身份伺服器中配置了一個客戶端應用程序,具有以下設置(僅顯示相關設置):

ClientId: mvc
ProtocolType: oidc
ClientSecret: K7gNU3sdo+OL0wNhqoVWhr3g6s1xYv72ol/pe/Unols=

(URLs to client app)
RedirectUri: https://localhost:44313/signin-oidc
PostLogoutRedirectUri: https://localhost:44313/signout-callback-oidc

GrantType: Hybrid

客戶編號

在此處輸入圖像描述

我的客戶端應用程序(伺服器端 Blazor 應用程序)在Startup.cs.

       // Add authentication
       services.AddAuthentication(options =>
       {
           options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
           options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
       })
       .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
       .AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
       {
           options.RequireHttpsMetadata = false;
           options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
           options.Authority = "http://localhost:5000/"; // local identity server url
           options.ClientId = "mvc";
           options.ClientSecret = "K7gNU3sdo+OL0wNhqoVWhr3g6s1xYv72ol/pe/Unols=";
           options.ResponseType = OpenIdConnectResponseType.CodeIdToken;
           options.SaveTokens = true;
           options.GetClaimsFromUserInfoEndpoint = true;
           options.Scope.Add("profile openid web_api");
       });

當我啟動我的客戶端應用程序時,我會重定向到我的 IdentityServer 登錄頁面。然後我可以使用使用者名和密碼登錄。當我登錄時,我會被重定向回我的客戶端應用程序https://localhost:44313/signin-oidc

但隨後我在該頁面上收到以下錯誤:

OpenIdConnectProtocolException:消息包含錯誤:‘invalid_client’,error_description:’error_description is null’,error_uri:’error_uri is null’。

對我來說,看起來我使用的是正確的ClientId

我究竟做錯了什麼?

ClientSecret應該包含未加密的值。看看文件

在你的情況下secret

options.ClientSecret = "secret";

我沒有進一步看,所以如果此更改不能解決問題,請告訴我。

請檢查客戶端配置(clientId),是否匹配給定的客戶端配置。

就我而言,問題與秘密有關。

2 秘密問題的注意事項:

  1. 在客戶端應用程序中,‘ClientSecret’ 應該是 ‘unencryptedvalue’ - 明文。(以下範例中的’secret’)
  2. 在為所有客戶端進行配置時,請在身份伺服器中檢查 secret.Type,它應該是“SharedSecret”。

例子:

Secret secret = new Secret("secret".Sha256(), "Description");
secret.Type = "SharedSecret";

引用自:https://stackoverflow.com/questions/58923358