我有 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] 來做我想做的事;
services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication(options =>
{
options.Authority = "https://demo.identityserver.io";
options.ApiName = "api1";
options.ApiSecret = "secret";
});
但是當我嘗試這個時,即使我的 clientSecret 不正確,我的 api 也不會回傳 401 Unauthorized 并回傳 200 OK。
我怎么解決這個問題 ?
uj5u.com熱心網友回復:
您在 API 中使用的 AddJwtBearer 身份驗證處理程式不需要 clientid/secret,因為它需要做的就是使用從 IdentityServer 發現檔案下載的公鑰接受和驗證傳入的訪問令牌。
如果您需要同時支持 JWT 和參考令牌,我認為 IdentityServer4.AccessTokenValidation 主要有用。但通常你只需要 JWT 令牌。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/516915.html
標籤:C#asp.net 核心oauth-2.0身份服务器4
