Asp.net core 如何在每次IdentityServer4 api调用中获取客户端信息?(用于记录)
我有一个受identityServer4保护的api:Asp.net core 如何在每次IdentityServer4 api调用中获取客户端信息?(用于记录),asp.net-core,asp.net-core-webapi,identityserver4,Asp.net Core,Asp.net Core Webapi,Identityserver4,我有一个受identityServer4保护的api: services.AddAuthentication(defaultScheme: IdentityServerAuthenticationDefaults.AuthenticationScheme) .AddIdentityServerAuthentication(options => { options.Authority = "https://localho
services.AddAuthentication(defaultScheme: IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication(options =>
{
options.Authority = "https://localhost:5000";
options.ApiName = "api1";
options.ApiSecret = "secret";
options.EnableCaching = true;
options.CacheDuration = TimeSpan.FromMinutes(10); // that's the default
});
// adds an authorization policy to make sure the token is for scope 'api1'
services.AddAuthorization(options =>
{
// some policies here
});
我想用客户机信息(如:ClientId、ClientName,
)记录每个客户机的api调用(不仅仅是身份验证)。
以下是我的拦截器中间件:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger logger)
{
app.UseHttpsRedirection();
app.Use(async (context, next) =>
{
// Log the incoming request here (e.g clientId, clientName, ...)
await next.Invoke();
});
// ...
}
// Write streamlined request completion events, instead of the more verbose ones from the framework.
// To use the default framework request logging instead, remove this line and set the "Microsoft"
// level in appsettings.json to "Information".
app.UseSerilogRequestLogging();
我该怎么做呢?您可以添加Serilog ASP.NET核心以记录所有详细信息 然后还可以使用SerilogRequestLogging中间件:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger logger)
{
app.UseHttpsRedirection();
app.Use(async (context, next) =>
{
// Log the incoming request here (e.g clientId, clientName, ...)
await next.Invoke();
});
// ...
}
// Write streamlined request completion events, instead of the more verbose ones from the framework.
// To use the default framework request logging instead, remove this line and set the "Microsoft"
// level in appsettings.json to "Information".
app.UseSerilogRequestLogging();
参见sameple代码我最终捕获了OAuth2事件:
options.OAuth2IntrospectionEvents.OnTokenValidated = async (context) =>
{
var identity = context.Principal.Identity as ClaimsIdentity;
identity.AddClaim(new Claim("my_claim", "claim_value"));
// ...
// log the request
}
不,我对NLog、Serilog等日志框架没有问题。我想拦截IS4 api调用。