且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Bearer令牌访问IdentityServer4上受保护的API

更新时间:2023-02-20 18:59:44

下面是一个在IdentityServer内部共同托管受保护API的示例:

There is an example co-hosting a protected API inside IdentityServer: IdentityServerAndApi

我与他们的启动公司之间的快速比较是,他们正在调用AddJwtBearer而不是AddIdentityServerAuthentication:

I quick comparison between their startup and yours is that they are calling AddJwtBearer instead of AddIdentityServerAuthentication:

services.AddAuthentication()
 .AddJwtBearer(jwt => {
    jwt.Authority = "http://localhost:5000";
    jwt.RequireHttpsMetadata = false;
    jwt.Audience = "api1";
});

Authorize属性还设置身份验证方案:

TheAuthorize attribute also sets the authentication scheme:

[Authorize(AuthenticationSchemes = "Bearer")]