JWT in front-end
Hi, I'm trying to use JWT as a auth for my app (because I also plan to create a mobile app)
So I've made two projects : Project.Api and Project.Web (which is basically just the front-end) but i don't know how to correctly setup jwt for the front end project
134 Replies
don't hesitate to ping or dm me
or using cookie is just easier ?
because idk if using cookies for a BFF structure is better
Where are you getting your JWT from? Are you using an identity provider or do you have your own auth api endpoint to consume it from?
What scheme do you want to consume the JWT as in your API as well?
If you have an external identity provider already that provides you with a JWT and you are using ASP.NET Core for the backend API then you could use the JWT Bearer scheme, this will allow you to pass the JWT through the Authorization header with the format
Authorization: Bearer JWT_HERE
You can also use HTTP only cookies but I believe when you are making requests from your frontend you will have to allow credentials in your request and you will need to parse the cookie yourself in the backend API
Also don't forget to create a CORS policy and apply it, otherwise you will not be able to make requestsExtra reading:
https://learn.microsoft.com/en-us/aspnet/core/security/authentication/configure-jwt-bearer-authentication?view=aspnetcore-9.0
https://learn.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-8.0
Configure JWT bearer authentication in ASP.NET Core
Learn how to set up JWT bearer authentication in an ASP.NET Core app.
Enable Cross-Origin Requests (CORS) in ASP.NET Core
Learn how CORS as a standard for allowing or rejecting cross-origin requests in an ASP.NET Core app.
well i was thinking about using aspnet core identity (ef) and i use the JwtDefaults.AuthenticationScheme value
okay thanks, im gonna check that tomorrow
I have been looking into similar things, and it seems like BFF + SessionCookie is easier if you don't have a full IDP (Okta, entra, Identiy Server, IAM, etc) (not an expert)
I have less than 100k users, and all of my webapps + apis are on the same domain so I am going to shoot for a session cookie set to Lax for my auth
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
okay it is what i did first but the issue is that it seems like the cookie isn't sent back
this is my API login action
and even tho credentials are valid and the
Ok()
is executed, the cookie isn't sent back to the browser
this is my front-side, nothing special
and this is my API's Program.cs file
the thing is that i also wanna create a mobile app that will connect to this api
i've tried jwt and jwt is kinda odd to me for the moment and i've used aspnet core identity cookies auth much longer but it was only on aspnetcore mvc appsUnknown User•2d ago
Message Not Public
Sign In & Join Server To View

it will look like that
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
no worries
answer me when you will be able to
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
okay
hold on
GitHub
GitHub - kubikpatrick/Poppsza
Contribute to kubikpatrick/Poppsza development by creating an account on GitHub.
thats it
what i want is the API handling the auth, data, db, etc and clients (desktop, mobile or web) to consume it by being logged it, that's it. and the front will, not be hosted on the same domain, usually it's gonna be hosted on localhost
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
idk why it's duplicated but those are just .editorconfig files
analyzers
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
vs did it
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
okay i didn't know that
thanks
well
what didnt u understand ?
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
so my Web also must have a auth setup?
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
AuthZ is authorization
authn is identity ?
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
i know the goals right
authn is to validate what users say they are
authz is to validate what users say they have access to
right?
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
yes
okay
what i wanna do
i’ll explain u what i wanna do
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
okay hold on
what i wanna do is self hosted a geolocation tracker
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
people host their API server and the web client
is supposed to connect to the API
and then to log in
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
like
API is where everything happens
clients (mobile, web) just consume it
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
nw mb
5 mins lemme take a shower
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
yes ik
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
oh
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
i see
just not practical
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
ohh
okay i see
so in my case
jwt is the best option ?
Unknown User•2d ago
Message Not Public
Sign In & Join Server To View
okay i see
so basically
ok just lemme explain so at least u can understand
the API is hosted online and clients (mobile, web, etc) should enter the API’s uri THEN login with an account email/password
but i just want to code the auth in the right way for every platform
the api is autonomous (db, ws, auth)
so u sayin that jwt the best way ?
i see
Unknown User•21h ago
Message Not Public
Sign In & Join Server To View
oh yes my bad
okay
5 misn
basically "localhost" can be the .Web
idk how to be clear it's actually really simple to understand but i cant explain it in the right way cause english ain't my first language
like api should be autonomous

clients may not be "hosted"
they are supposed to work normally even in localhost as long as the API is accessible
ykwim
Unknown User•20h ago
Message Not Public
Sign In & Join Server To View
yes
i will require an account for EVERY client
they are not supposed to be anonymous at all
i've actually coded some shitty ass jwt auth
Unknown User•20h ago
Message Not Public
Sign In & Join Server To View
wdym
Unknown User•20h ago
Message Not Public
Sign In & Join Server To View
no
an online provider?
no
like auth0?
Unknown User•20h ago
Message Not Public
Sign In & Join Server To View
i don't think so, i think it'll complicate the deployment for those who want to use the app
Unknown User•20h ago
Message Not Public
Sign In & Join Server To View
ahh okay i got it
yeah i'm my own authority
im signing it, validating it

u're talking about this right ?
Unknown User•20h ago
Message Not Public
Sign In & Join Server To View
why
Unknown User•20h ago
Message Not Public
Sign In & Join Server To View
yeah i forgot the cookie auth
it was kinda complicated and i think jwt is the "easiest" way for my case
Unknown User•20h ago
Message Not Public
Sign In & Join Server To View
forget about the cookie
i just setup identity for UserManager<User>, passwords hashing etc
but i don't use cookies anymore
Unknown User•20h ago
Message Not Public
Sign In & Join Server To View
yeah it is if Authorization: Bearer <blablaba> header is present and validated
Unknown User•20h ago
Message Not Public
Sign In & Join Server To View
aspnet core identity handles jwt ?
Unknown User•20h ago
Message Not Public
Sign In & Join Server To View
my main issue right now is not the API
the api works fine, IP works fine also
[Authorize] controllers cannot be accessed without a valid jwt right
i have some issues with the frontend (.Web), clients basically
because i think i kinda messed up somewhere
hold on i'll push everything
GitHub
GitHub - kubikpatrick/Poppsza
Contribute to kubikpatrick/Poppsza development by creating an account on GitHub.
https://github.com/kubikpatrick/Poppsza/blob/9796c67a91a462e4ddde2587fc7e2262fccafd05/Poppsza.Api/Controllers/AuthController.cs#L91
there especially idk if it's the standard way
GitHub
Poppsza/Poppsza.Api/Controllers/AuthController.cs at 9796c67a91a462...
Contribute to kubikpatrick/Poppsza development by creating an account on GitHub.
but it works
OH GOD DAMN
LOOK AT THAT


DAMN
IT
IS
WOR
KING
@TeBeCo look at this shit
but it only works for the back
that's better than nothing but
for the front i also want my User.Identity.IsAuthenticated to work
Unknown User•19h ago
Message Not Public
Sign In & Join Server To View
okay i think i got it
well the human user is signing in through the web client that will get the account from the api
idk if it’s clear
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
no worries ik my explanations arent obvious too
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
no i wont deploy anything
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
Api is self hosted by the users
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
open source shit
YES
exactly
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
no no i also write clients
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
free open source
i don’tplan to earn any money from it
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
i guess they will have some basic IT knowledge like JWT auth, URI binding
i mean they’re not supposed to know nothing
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
no no desktop app will just be a WPF client i think
they wont have to deploy any clients
they can if they want to but it’s not mandatory
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
i mean they will just download the .exe ig idk
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
yeah that’s what i was planning
but it’s not for now right
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
3 tokens?
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
yeah that’s what i wanted to do
idk i just want to implement that in the best way
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
hmmm what i did actually
is JWT for the back right that returns the generated token to the front
/auth/login returns a token
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
the front stores it in a cookie
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
well idk i haven’t thought about it yet
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
yeah lmaoo
euh
first
is that done in a correct way?
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
yeah
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
yeah as a cookie
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
yeah mobile is gonna come later
okay so at least this is correct
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
this is another issue lmao
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
yeah i know
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
it sounds logical
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
i was thinking about a distributed cache
like redis
Unknown User•18h ago
Message Not Public
Sign In & Join Server To View
for cookies?
i mean the app is not meant to be distributed now
i focus on a functional auth
and later i’ll see how to set everything that up
i gotta need to find some map tile provider
@TeBeCo okay
i have found a solution for my
User.Identity.IsAuthenticated
and it was pretty easy
i just had to setup a cookie auth for my frontUnknown User•12h ago
Message Not Public
Sign In & Join Server To View
yeah
that shit is magic
my mom's gonna be proud