如何在每个 IdentityServer4 api 调用中获取客户端信息? (用于记录目的)
How to get client information in every IdentityServer4 api calls? (for logging purpose)
我有一个 api 受 identityServer4 保护:
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
});
我想记录每个客户的api呼叫(不仅仅是身份验证) 与客户信息如:ClientId, ClientName, ...
。
这是我的拦截器中间件:
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();
});
// ...
}
我该怎么做?
您可以添加 Serilog ASP.NET 核心 NuGet package 以记录所有详细信息。
然后同样使用SerilogRequestLogging中间件:
// 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();
参见相同代码here
我最终捕获了 OAuth2 事件:
options.OAuth2IntrospectionEvents.OnTokenValidated = async (context) =>
{
var identity = context.Principal.Identity as ClaimsIdentity;
identity.AddClaim(new Claim("my_claim", "claim_value"));
// ...
// log the request
}
我有一个 api 受 identityServer4 保护:
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
});
我想记录每个客户的api呼叫(不仅仅是身份验证) 与客户信息如:ClientId, ClientName, ...
。
这是我的拦截器中间件:
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();
});
// ...
}
我该怎么做?
您可以添加 Serilog ASP.NET 核心 NuGet package 以记录所有详细信息。
然后同样使用SerilogRequestLogging中间件:
// 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();
参见相同代码here
我最终捕获了 OAuth2 事件:
options.OAuth2IntrospectionEvents.OnTokenValidated = async (context) =>
{
var identity = context.Principal.Identity as ClaimsIdentity;
identity.AddClaim(new Claim("my_claim", "claim_value"));
// ...
// log the request
}