Help making login function c#
Im currently trying to code a login function
I have the registration my user is getting saved in the db!
So now i want to check the user(I am using Asp.Net Web Api for it) my api is connected to my frontend and like i said the user is being saved in the db. i wrote that method for the login in my api(see picture) and idk how i could bind that in my frontend...
41 Replies
Well, your method doesn't check anything useful right now
So there's that
For a "binding"... what do you mean?
You call this API endpoint with
fetch
or axios
or whatever
And do whatever with the data you get backuse ur DbContext or whatever to match user data via DI
Basically i mean i can post my user and he's getting saved but i can tget it how i could check that he existing i tried severall methods(Pictures of the frontend code)
also why are your users provide password hash insted of simply pass? It is better to create new DTO model which represents login request with needed data. And then you can map it to next layer of your logic
You check that a given user exists and has the correct password by... checking that
1. Get the user by username from the database
2. Hash the provided password with the retrieve'd user's salt
3. Compare the hashes
i did something else before i forgot to change the name 😦
I dont do something now with hash
i changed my program
i now just want to have a login and sign up
Also, you can try something called Identity https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-8.0&tabs=visual-studio
it does a lot of magic that usually helps with sign in/up
Do you not hash the passwords?
Do you store them in the database as plain text?
yeah right. I hashed them before but now i changed the whole program because of that i just want to check my user
Well, then I hope you don't plan to actually let anybody get even close to this project
Because hashing passwords is security 101
here i just trying to send the user(which works) and to check the user (were i have problems)
Uh
no its just for educational purposes
Just... check the user exists?
yes
There
That's how you use EF to check if a thing that matches the predicate exists
and for the password the same
i guess thats fot the api right?
Just be advised that it's shit code
That should never have been created
It's for whatever touches the database
If it's the API, then yes, it's for the API
Okay thx i will try it out
I recommend learning LINQ
At least the basics, like...
.Any()
And .Select()
, and .ToList()
, etc
You won't go far with EF if you don't know LINQYeah im learning the basics now of linq
but i have some errors XD
username and password are unknown what should be there
?
The username
And the password
Wherever you get them from
So assuming this is your code still...
User.Username
and User.PasswordHash
yeah know thats work but
User.Username = username(<- here is the error in little user name he cant find it)
Wh
what
This is how you do it
I guess add anonymous functions/lambdas/closures to your learning list
IDE is german thats why i cant send you the error
Fixed a typo
Should be
==
of course
Not =
bruh he stillst gives out a error
Adjust my code to your code
Maybe you don't have a
DbSet
called Users
Maybe the password property on your user model is not named Password
Maybe your dbcontext is not in a _context
field but in ctx
Maybe you haven't injected it
Don't take this code literally, it only shows the principle
Adjust accordinglyGreat
You know what to adjust
lowercase, should be
user
- not User
i just saw it
XDD
yeah now it works
now i need my method CheckUser to return
something
should i just return Ok(exists)
better to do:
Okay thxx
May I ask you what does
/Login
do? It should just check existence or returning some data like a tokens?but should i do in my front end
yeah its checks the existence
i think login is not the right name for it
Yeah, you should change
You should just call this endpoint and provide
User
as JSON string in bodyalso it is better to specify it:
your signature should looks like this if I understood u
HttpClient.PostAsync Method (System.Net.Http)
Send a POST request to the specified Uri as an asynchronous operation.