I did this myself recently and it's a bit of a pain. Interestingly, largely due to the limitations on the extension method setup methods. Which which restrict your control over the component. Forcing, forcing you to use CookieAuth when really you don't really want to.
What I setup was Cookie, JWT and OpenIdConnect authentication. in the cookie auth we forward events to the JWT or OpenId auth
.AddCookie(options =>
{
options.ForwardAuthenticate = JwtBearerDefaults.AuthenticationScheme;
options.ForwardChallenge = OpenIdConnectDefaults.AuthenticationScheme;
options.Events = new CookieAuthenticationEvents()
{
OnRedirectToAccessDenied = async ctx =>
{
ctx.Response.StatusCode = 401;
},
};
})
In the JWT auth we check our own cookie:
options.Events = new JwtBearerEvents()
{
OnMessageReceived = async ctx =>
{
//get the token from the cookie rather than the header
var token = ctx.HttpContext.Request.Cookies["access_token"];
ctx.Token = token;
},
};
In the OpenId auth we set the cookie
ctx.Response.Cookies.Append("access_token", tokenResponse.AccessToken);