我的网络应用控制器上有[授权]属性,因此任何端点命中都会确保用户首先被重定向到登录OAuth服务器(如果尚未登录).
我现在想要在用户每次登录时开始将用户声明写入Web应用程序数据库.为此,我需要在每次用户成功登录/授权时在Web应用程序上运行一些代码.
我得到了一个线索,它涉及添加自定义中间件.
我的启动ConfigureServices代码目前如下:
public class Startup
{
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
Configuration = configuration;
Env = env;
}
public IHostingEnvironment Env { get; }
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
// Adds a default in-memory implementation of IDistributedCache.
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.Cookie.HttpOnly = true;
});
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options =>
{
options.SignInScheme = "Cookies";
options.Authority = Configuration["auth:oidc:authority"];
options.RequireHttpsMetadata = !Env.IsDevelopment();
options.ClientId …Run Code Online (Sandbox Code Playgroud) c# asp.net-mvc asp.net-core identityserver4 asp.net-core-middleware