Skip to main content
deleted 15 characters in body
Source Link
Robert Harvey
  • 200.1k
  • 55
  • 468
  • 679

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);

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 restrict your control over the component. Forcing you to use CookieAuth when really you don't 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);

I did this myself recently and it's a bit of a pain, largely due to the limitations on the extension method setup methods which restrict your control over the component, forcing you to use CookieAuth when 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);
Source Link
Ewan
  • 79.8k
  • 5
  • 83
  • 179

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 restrict your control over the component. Forcing you to use CookieAuth when really you don't 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);