C
C#3mo 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
Angius3mo 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
kurumi3mo ago
use ur DbContext or whatever to match user data via DI
IbraDev💻
IbraDev💻3mo 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
kurumi3mo 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
Angius3mo 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💻3mo 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
kurumi3mo 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
Angius3mo ago
Do you not hash the passwords? Do you store them in the database as plain text?
IbraDev💻
IbraDev💻3mo 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
Angius3mo 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💻3mo ago
here i just trying to send the user(which works) and to check the user (were i have problems)
No description
Angius
Angius3mo ago
Uh
IbraDev💻
IbraDev💻3mo ago
no its just for educational purposes
Angius
Angius3mo ago
Just... check the user exists?
IbraDev💻
IbraDev💻3mo ago
yes
Angius
Angius3mo 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💻3mo ago
and for the password the same i guess thats fot the api right?
Angius
Angius3mo 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💻3mo ago
Okay thx i will try it out
Angius
Angius3mo 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💻3mo 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
Angius3mo 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💻3mo ago
yeah know thats work but User.Username = username(<- here is the error in little user name he cant find it)
Angius
Angius3mo 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💻3mo ago
IDE is german thats why i cant send you the error
Angius
Angius3mo ago
Fixed a typo Should be == of course Not =
IbraDev💻
IbraDev💻3mo ago
bruh he stillst gives out a error
Angius
Angius3mo 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💻3mo ago
No description
Angius
Angius3mo ago
Great You know what to adjust
IbraDev💻
IbraDev💻3mo ago
No description
kurumi
kurumi3mo ago
lowercase, should be user - not User
IbraDev💻
IbraDev💻3mo 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
kurumi3mo ago
better to do:
return exists ? Ok() : NotFound();
return exists ? Ok() : NotFound();
IbraDev💻
IbraDev💻3mo ago
Okay thxx
kurumi
kurumi3mo ago
May I ask you what does /Login do? It should just check existence or returning some data like a tokens?
IbraDev💻
IbraDev💻3mo 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
kurumi3mo ago
Yeah, you should change You should just call this endpoint and provide User as JSON string in body
IbraDev💻
IbraDev💻3mo ago
No description
kurumi
kurumi3mo 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
More Posts
Problems with docker and my app imageI already posted a question that more or less manage to solve but now i have other problem, the thiAttempting to get value from different JSON files with variable keyHello o/ I am attempting to deserilize and obtain the value from a variety of JSON files that contaiHow to display only department fields associated with a selected department in student automationhi, I'm facing a challenge while working on the student automation system. After selecting the deparHelp with DiscordGameSDK in a C# project (no Unity)I am currently trying to develop a plugin for a Unity game. For this I created a C# project, where IMarshal.GetActiveObject("Word.Application") fall in exceptionI'm working on a `.NET Framework` console application, I have added the `Microsoft.Office.Interop.WMigrating to Microsoft.IdentityModel.JsonWebTokens .net6 => .net8Hi, I am migrating a project which uses Duende as identity server + Ocelot gateway from .NET 6 to .NProcess working set is twice as commited managed memoryHi! I have an ASP.NET Core 8 app running on Linux in Docker. It's memory usage is limited by 512M. Resource based authorization with IdentityHello, I have the following setup in my asp.net core project: A `Workspace` model that has a bunch o✅ is .net 8.0 is not compatibleHey hi guys I'm new to this group and .net I just started learning .net I don't have any experience Help with why code is returning this way.Hello, I'm a CS student learning C# for the first time and i'm having some confusion on why my code