【发布时间】:2021-12-03 12:01:59
【问题描述】:
服务器
使用 IdentityServer3 进行客户端/应用程序授权。
使用 IdentityAdmin 通过 GUI 编辑客户端/范围。
为 API 创建了一个新客户端,添加了 SharedSecret 和 api 范围。
API / 客户端
有 2 个 GET 端点。
使用 IdentityServer4.AccessTokenValidation NuGet 包。
配置应该很简单:
Startup.cs
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();
});
}
测试:
- 从身份“/connect/token”端点获取客户端引用令牌
- GET API 的端点添加了标头“Authorization: Bearer {token}”
- 收到 401 Unauthorized
我尝试过的事情:
- 不同的 Startup.cs 配置
- 尝试通过身份“/connect/accesstokenvalidation”端点验证令牌,令牌有效。
- 不同的 apiname/apisecret 值,因为不能 100% 确定它们必须是什么。
- 谷歌搜索无济于事
我在这里不知所措,我做错了什么吗?这只是一个兼容性问题吗?还是我根本什么都不懂?似乎缺乏清晰的文档,用户必须提取信息。
使用的来源
https://github.com/IdentityServer/IdentityServer4.AccessTokenValidation
IdentityServer3 文档
SO / github/identityserver3 个线程。
【问题讨论】:
标签: c# .net access-token .net-5 identityserver3