Open MadL1me opened 1 year ago
It's unclear to me in the above proposal what the middleware would do, and what the end-to-end flow looks like.
It's possible today to customize where the JWT comes from:
builder.Services.AddAuthentication().AddJwtBearer(o =>
{
o.Events = new()
{
OnMessageReceived = context =>
{
// Get the token from a cookie
context.Token = context.Request.Cookies[".AspNetCore.Application.Id"];
return Task.CompletedTask;
}
};
});
PS:
This is already doable with what ASP.NET Core ships today. You can see it here:
The JWT token is being acquired from another server in this situation, but you can see that all of the pieces are there.
@davidfowl thank you. I tried to search, but didn't found the solution you provided anywhere in the microsoft docs. Again, thanks for help, and sorry for issue, I thought it wasn't possible currently
@Rick-Anderson Do we document this particular scenario for OnMessageReceived
to incorporate JWT token derived from a cookie?
There are some docs on this pattern in https://learn.microsoft.com/en-us/aspnet/core/signalr/authn-and-authz?view=aspnetcore-7.0#built-in-jwt-authentication.
@Rick-Anderson Do we document this particular scenario for
OnMessageReceived
to incorporate JWT token derived from a cookie?
No.
There are some docs on this pattern in https://learn.microsoft.com/en-us/aspnet/core/signalr/authn-and-authz?view=aspnetcore-7.0#built-in-jwt-authentication.
Where should this information go? I don't see a doc where it could be added.
I think adding it to the same heading linked above should be sufficient.
I think adding it to the same heading linked above should be sufficient.
But that's SignalR, as you asked
@Rick-Anderson Do we document this particular scenario for OnMessageReceived to incorporate JWT token derived from a cookie?
How will non-SignalR folks find it?
@adityamandaleeka can you assign someone to draft how to use OnMessageReceived
to incorporate JWT token derived from a cookie?
@HaoK Can you help with that?
FYI @Rick-Anderson The auth space is now owned by @rafikiassumani-msft's team.
FYI @Rick-Anderson The auth space is now owned by @rafikiassumani-msft's team.
Can you update Team Ownership? That's what I use.
Will do.
cc: @JeremyLikness
There's questions here about where to document a scenario that users inquired about. Bringing it to your radar because I'm not sure where the best place in the docs to place it is.
Will address this as part of a broader effort to improve auth-related docs and will cross-link when the other issue is available.
Summary
Create in-box support for Authn with JWT stored in http-only cookies
Motivation and goals
Then we develop SPA application with ASP.NET Core (for ex. with React at frontend) we very often think about how we implement authentication in our application. There is currently two ways to do it: with session cookie, and with JWT. As devs, we prefer to use jwt, because it does not require for a DB lookup.
If we decided to use jwt, there is 3 ways to do request with it:
Authorization: Bearer <token>
) and store it locally in memory (.AddJwtBearer()
Authorization: Bearer <token>
) and store it in browser local storage (.AddJwtBearer()
)The third option is currently the most secure way to pass and store jwt tokens, because we become immutable to XSS attack, because attacker cannot read our token from cookie. Still, we became vulnerable to CSRF, but this we can fix this by providing XSRF token on a client side. By the end of a day, we can see why JWT with http-only secure cookie is preferred as authn solution using jwt's.
However this way is most secure, ASP.NET Core does not provide in-box solution for this, so we need to add custom middleware to any project which uses this technique. Currently, we implement this logic by this sort of middleware:
Futhermore, a lot of other tech solutions (such as NextAuth) already have support for this feature.
Goals:
In scope
A list of major scenarios, perhaps in priority order.
Out of scope
Scenarios you explicitly want to exclude.
Risks / unknowns
There is security concern, and we need to be careful about reviewing PR for this. Also,
Examples
Design proposal No.1: New middleware
We can add new middleware, which will
Design proposal No.2: Different authentication scheme?
I'm not sure, if that can be considered as a different authn scheme. If so, we would be able to add jwt in cookie alongside with default jwt bearer and cookies:
Associated WorkItem - 55240