C
C#9mo ago
IbraDev💻

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...
No description
41 Replies
Angius
Angius9mo ago
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 back
kurumi
kurumi9mo ago
use ur DbContext or whatever to match user data via DI
IbraDev💻
IbraDev💻OP9mo ago
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)
No description
kurumi
kurumi9mo ago
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
Angius
Angius9mo ago
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
IbraDev💻
IbraDev💻OP9mo ago
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
kurumi
kurumi9mo ago
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
Angius
Angius9mo ago
Do you not hash the passwords? Do you store them in the database as plain text?
IbraDev💻
IbraDev💻OP9mo ago
yeah right. I hashed them before but now i changed the whole program because of that i just want to check my user
Angius
Angius9mo ago
Well, then I hope you don't plan to actually let anybody get even close to this project Because hashing passwords is security 101
IbraDev💻
IbraDev💻OP9mo ago
here i just trying to send the user(which works) and to check the user (were i have problems)
No description
Angius
Angius9mo ago
Uh
IbraDev💻
IbraDev💻OP9mo ago
no its just for educational purposes
Angius
Angius9mo ago
Just... check the user exists?
IbraDev💻
IbraDev💻OP9mo ago
yes
Angius
Angius9mo ago
var exists = await _context.Users.AnyAsync(u => u.Username = username);
var exists = await _context.Users.AnyAsync(u => u.Username = username);
There That's how you use EF to check if a thing that matches the predicate exists
IbraDev💻
IbraDev💻OP9mo ago
and for the password the same i guess thats fot the api right?
Angius
Angius9mo ago
var exists = await _context.Users.AnyAsync(u => u.Username = username && u.Password = password);
var exists = await _context.Users.AnyAsync(u => u.Username = username && u.Password = password);
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
IbraDev💻
IbraDev💻OP9mo ago
Okay thx i will try it out
Angius
Angius9mo ago
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 LINQ
IbraDev💻
IbraDev💻OP9mo ago
Yeah im learning the basics now of linq but i have some errors XD username and password are unknown what should be there ?
Angius
Angius9mo ago
The username And the password Wherever you get them from So assuming this is your code still... User.Username and User.PasswordHash
IbraDev💻
IbraDev💻OP9mo ago
yeah know thats work but User.Username = username(<- here is the error in little user name he cant find it)
Angius
Angius9mo ago
Wh what
var exists = await _context.Users.AnyAsync(u => u.Username == User.Username && u.Password == User.PasswordHash);
var exists = await _context.Users.AnyAsync(u => u.Username == User.Username && u.Password == User.PasswordHash);
This is how you do it I guess add anonymous functions/lambdas/closures to your learning list
IbraDev💻
IbraDev💻OP9mo ago
IDE is german thats why i cant send you the error
Angius
Angius9mo ago
Fixed a typo Should be == of course Not =
IbraDev💻
IbraDev💻OP9mo ago
bruh he stillst gives out a error
Angius
Angius9mo ago
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 accordingly
IbraDev💻
IbraDev💻OP9mo ago
No description
Angius
Angius9mo ago
Great You know what to adjust
IbraDev💻
IbraDev💻OP9mo ago
No description
kurumi
kurumi9mo ago
lowercase, should be user - not User
IbraDev💻
IbraDev💻OP9mo ago
i just saw it XDD yeah now it works now i need my method CheckUser to return something should i just return Ok(exists)
kurumi
kurumi9mo ago
better to do:
return exists ? Ok() : NotFound();
return exists ? Ok() : NotFound();
IbraDev💻
IbraDev💻OP9mo ago
Okay thxx
kurumi
kurumi9mo ago
May I ask you what does /Login do? It should just check existence or returning some data like a tokens?
IbraDev💻
IbraDev💻OP9mo ago
but should i do in my front end yeah its checks the existence i think login is not the right name for it
kurumi
kurumi9mo ago
Yeah, you should change You should just call this endpoint and provide User as JSON string in body
IbraDev💻
IbraDev💻OP9mo ago
No description
kurumi
kurumi9mo ago
also it is better to specify it:
[HttpPost("Existence")]
public async Task<IActionResult> CheckUserExistence([FromBody] User user)
[HttpPost("Existence")]
public async Task<IActionResult> CheckUserExistence([FromBody] User user)
your signature should looks like this if I understood u
Want results from more Discord servers?
Add your server