同一项目中的 IdentityServer4 令牌发行者和消费者
IdentityServer4 token issuer and consumer in the same project
我有一个托管 IdentityServer4 的项目,我正尝试在同一个项目中托管一个 Web API,它接受访问令牌。
我的问题是,单个项目是否可能包含使用相同 IdentityServer 的 IdentityServer 和 Web API?
编辑:API 必须使用授权属性
进行保护
我有一个身份服务器 4 项目,在同一个项目中有一个 API 用于客户端的 CIUD。 (我们称之为开发者控制台 api)。
然后我有一个附带项目,它是一个 asp .net 核心项目,其中包含开发人员控制台的实际剃刀页面,它访问身份服务器项目中的 API。
我这样做的原因是只有一个项目应该更新数据库。因此,为了更新身份服务器拥有的数据库,决定访问它的 API 也应该在同一个项目中。
是的,您可以在您的 Identity Server 4 项目中创建一个网络 api。
配置服务
services.AddAuthentication(IdentityServerConstants.DefaultCookieAuthenticationScheme)
.AddIdentityServerAuthentication(options =>
{
// base-address of your identityserver
options.Authority = settingsSetup.Settings.Authority;
// name of the API resource
options.ApiName = "testapi";
options.RequireHttpsMetadata = false;
});
配置
我认为这两者都需要。
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
app.UseAuthentication();
app.UseIdentityServer();
端点
因为请求是使用访问令牌作为承载令牌发送的,所以每个 API 调用的授权都需要包含 authencationScheme。我还没有完全弄清楚为什么但是没有这个它就不起作用。
[HttpGet("Client/List")]
[Authorize(AuthenticationSchemes = "Bearer")]
public ActionResult ClientList()
{
}
虽然@DaImTo 的答案是正确的并且有效,它是由 IdentityServer 团队开发的,但它使用 Introspection Endpoint
这意味着对于每个请求 AddIdentityServerAuthentication
都会创建一个 http 请求并将其发送到您的服务器,这是同一个应用。
我开发了一个名为 IdentityServer4.Contrib.LocalAccessTokenValidation 的库,它做完全相同的事情但没有使用 Introspection Endpoint
。它将直接从服务中配置的 TokenStore
验证令牌。有兴趣的可以用。
nuget link : https://www.nuget.org/packages/IdentityServer4.Contrib.LocalAccessTokenValidation
github link : https://github.com/Kahbazi/IdentityServer4.Contrib.LocalAccessTokenValidation
我有一个托管 IdentityServer4 的项目,我正尝试在同一个项目中托管一个 Web API,它接受访问令牌。
我的问题是,单个项目是否可能包含使用相同 IdentityServer 的 IdentityServer 和 Web API?
编辑:API 必须使用授权属性
进行保护我有一个身份服务器 4 项目,在同一个项目中有一个 API 用于客户端的 CIUD。 (我们称之为开发者控制台 api)。
然后我有一个附带项目,它是一个 asp .net 核心项目,其中包含开发人员控制台的实际剃刀页面,它访问身份服务器项目中的 API。
我这样做的原因是只有一个项目应该更新数据库。因此,为了更新身份服务器拥有的数据库,决定访问它的 API 也应该在同一个项目中。
是的,您可以在您的 Identity Server 4 项目中创建一个网络 api。
配置服务
services.AddAuthentication(IdentityServerConstants.DefaultCookieAuthenticationScheme)
.AddIdentityServerAuthentication(options =>
{
// base-address of your identityserver
options.Authority = settingsSetup.Settings.Authority;
// name of the API resource
options.ApiName = "testapi";
options.RequireHttpsMetadata = false;
});
配置
我认为这两者都需要。
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
app.UseAuthentication();
app.UseIdentityServer();
端点
因为请求是使用访问令牌作为承载令牌发送的,所以每个 API 调用的授权都需要包含 authencationScheme。我还没有完全弄清楚为什么但是没有这个它就不起作用。
[HttpGet("Client/List")]
[Authorize(AuthenticationSchemes = "Bearer")]
public ActionResult ClientList()
{
}
虽然@DaImTo 的答案是正确的并且有效,它是由 IdentityServer 团队开发的,但它使用 Introspection Endpoint
这意味着对于每个请求 AddIdentityServerAuthentication
都会创建一个 http 请求并将其发送到您的服务器,这是同一个应用。
我开发了一个名为 IdentityServer4.Contrib.LocalAccessTokenValidation 的库,它做完全相同的事情但没有使用 Introspection Endpoint
。它将直接从服务中配置的 TokenStore
验证令牌。有兴趣的可以用。
nuget link : https://www.nuget.org/packages/IdentityServer4.Contrib.LocalAccessTokenValidation
github link : https://github.com/Kahbazi/IdentityServer4.Contrib.LocalAccessTokenValidation