Need help with "creating an authentication cookie"
I'm reading the documentation of Microsoft of "Use cookie authentication without ASP.NET Core Identity":
https://learn.microsoft.com/en-us/aspnet/core/security/authentication/cookie?view=aspnetcore-9.0
It's my first time trying to follow documentation and implement something that fits my program.
I'm in the stage of "Create an authentication cookie" and this is Microsoft implementation:
The problems are:
(1) I don't use razor pages. I'm using React and backend.
(2) My plan is: The user to login from the website -> Sends a POST request with HTTP to the backend which uses my controller method -> Then I want to use this method to create the cookie -> Send it back to the frontend.
I don't know what I need to change and how.
Use cookie authentication without ASP.NET Core Identity
Learn how to use cookie authentication without ASP.NET Core Identity.
25 Replies
I suppose I need to totally change the method? What type I need to return? what I need to get as parameters?
Doubt you'll have to change a lot to be honest! But first a few control questions
Is your FE and BE served from the same host? If not, the cookie created by your BE might not be accessible from your FE
Its fairly common to have your BE and FE not be the same during development but actually be the same when built for production (using something like ASP.NETs serve static files), which might complicate things
Anyways, I'll post a working backend implementation here for you to look at
Program.cs
AuthController.cs
@Pobiega
I'm very sorry, I didn't hear any notification from discord, it's always making a sound and show me visual notification on the phone.
I'll look at your messages very soon, I'm fixing something else on my project and I'll be back to this.
Thank you very much, I appreciate it ❤️
I'll work on it in around 1 hour
Hey
BE = ASP.NET
FE = React
I don't use Razor since I'm a begginer, I didn't want to start with something complicated until I have good foundations
Yeah I got that part from your first post
but how do you host your react app?
what I usually do (during dev) is run my FE separately using
vite or similar, and use a dev server proxy to let the FE talk to BEVSC with vite
but during prod, I use ASP.NETs
UseStaticFiles to host the built FEMy FE and BE already communicate through database (Postgre) and http calls, isn't that enough?
The guide I sent here doesn't suppose to work? because I have separated development languages? Is it meant only for Razor pages?
I'm trying to make sure I understand you
The guide you linked is specifically for Razor yes, but as I showed above, its quite easily adapted for non-razor
What you are not getting is how cookies work
cookies is part of the http protocol and not something ASP or react decide what to do with, and part of that is whats called domain restriction
So should I follow the guide and change it compared to yours?
Because I'm trying to learn the process, otherwise it will be like magic and I won't understand anything.
I'm scared of not understanding anything
if I have a website called
www.pobiega.com I cant read cookies from www.google.comRight
so if your FE is on
localhost:9000 and your BE is on localhost:7000.... they cant read eachothers cookies either, I'd imagine
Cookie auth is pretty unusual for SPA apps, I'd say they mostly use some form of bearer tokens, usually JWTs
There might be a workaround thou, I'm not an expert.
a quick google search tells me Vite has a fix for thisMatthew Sullivan
Matt's Life Bytes
Unbreaking Cookies in Local Dev with Vite Proxy
Vite is a popular and powerful local dev server. One of my favorite Vite features is the proxy, which allows you to develop your frontend locally against an arbitrary backend URL. I found myself in…
So can you just explain me what your implementation does? and why it works?
What you implemented that the the guide didn't give
can't you do the comparison yourself?
I just removed the razor specific parts
Okie
I will
ie
Page, ControllerWithViews etc
and my auth controller is the simplest possible implementation of "a way to log in"
it doesnt check that the user is valid, has the right password, password hashing... nothing
it just says "sure you are now authenticated!"
and the GET endpoint is just to show that "it worked"
if you hook this up with your react app, it may or may not work
I suggest you try thatI will, hopefully everything works fine 😂
Thank you very much for your help and time!
removing the secure flag on the cookie might be enough
maybe tweaking the SameSite policy too
again, idk
I don't use cookie auth in SPA apps ;D
So what do you use?
Maybe I can get a better idea
Bearer token
specifically JWTs, but thats not really important
Okie!
Will play with what you gave me , hopefully it will work for now
something like this

Tbh that's what I wanted at the beginning, but didnt find any guide to do this where I didn't implement my whole models (User identity, etc...) with JWT.
The cookies is the first thing I found that didn't force me to implement my models with JWT(or any other algorithm).
I just need a way to send data of login user to the frontend, but seems like there are not many options