I have an [Authorize] attribute on my web app controller so any endpoints hit ensure user is re-directed to login on OAuth server first (if not already logged in.)
I now want to start writing user claims to the web app db every time a user logs in. To do this I need to have some code that runs on the web app every time a user is successfully logged in / authorised.
I have been given a clue that it involves adding custom middleware.
My Startup ConfigureServices code is currently as follows:
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 = Configuration["auth:oidc:clientid"];
options.ClientSecret = Configuration["auth:oidc:clientsecret"];
options.ResponseType = "code id_token";
options.Scope.Add(Configuration["auth:oidc:clientid"]);
options.Scope.Add("offline_access");
options.GetClaimsFromUserInfoEndpoint = true;
options.SaveTokens = true;
});
}
... []
So my question: what code do I need to add, and where, in order to call a method that will contain my custom action?