![](/img/trans.png)
[英]Identityserver4 with IdentityServer3 AccessTokenValidation
[英]Trouble authorizing .NET 5 Web API reference tokens to IdentityServer3 using IdentityServer4.AccessTokenValidation package
使用 IdentityServer3 進行客戶端/應用程序授權。
使用 IdentityAdmin 通過 GUI 編輯客戶端/范圍。
為 API 創建了一個新客戶端,添加了 SharedSecret 和 api 范圍。
有 2 個 GET 端點。
使用 IdentityServer4.AccessTokenValidation NuGet 包。
配置應該很簡單:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers(c => {
var policy = ScopePolicy.Create("api");
c.Filters.Add(new AuthorizeFilter(policy));
});
services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication(options => {
options.Authority = "{base url of identity server}";
options.ApiName = ""; // not sure what this is? client id from identity server?
options.ApiSecret = ""; // should this be the hashed password?
options.LegacyAudienceValidation = true;
});
services.AddSwaggerGen(c => {
c.SwaggerDoc("v1", new OpenApiInfo { Title = "MarvalAPI", Version = "v1" });
});
RegisterServices(services);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "MarvalAPI v1"));
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication(); //this is added, everything else is by default
app.UseAuthorization();
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
});
}
測試:
我嘗試過的事情:
我在這里不知所措,我做錯了什么嗎? 這只是兼容性問題嗎? 還是我根本就什么都不懂? 似乎缺乏清晰的文檔,用戶必須提取信息。
https://github.com/IdentityServer/IdentityServer4.AccessTokenValidation
IdentityServer3 文檔
SO / github/identityserver3 線程。
好吧,發表這篇文章一段時間后,我想通了。
options.ApiName = "";
options.ApiSecret = "";
ApiName是客戶端使用的范圍的名稱,因此在這種情況下,該值應為api 。
ApiSecret是范圍 secret的PRE-HASHED值。
例如,如果秘密值是“測試”並且它的 SHA256 值是 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08,那么ApiSecret值應該是測試
因此,在弄清楚這一點之后,上面的選項配置應該是這樣的:
options.ApiName = "api";
options.ApiSecret = "test";
注意: SHA512 也適用。
對我來說,這似乎是一個主要的命名問題。
我在分析了這個 VS 解決方案后解決了這個問題:
https://github.com/IdentityServer/CrossVersionIntegrationTests
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.