Home > Blockchain >  What would be the best/simplest solution to retrieve values from my database for comparison? - ASP.N
What would be the best/simplest solution to retrieve values from my database for comparison? - ASP.N

Time:08-11

I'm currently stuck on accessing all of the 'UserName' values from my database. I am doing this so I can compare the user input for a username to check if it has been used before (I don't want two instances of the same username). This is on a .cshtml.cs page. I am already able to access the database through my program, as create commands have been tested and do work. My program is on ASP.NET 6.0 Core Web App.

I am a student with basic knowledge on ASP.NET Core, and on how to solve this issue, therefore as much simplified explanation would be very appreciated.

Here is my code:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using AQA_A_Level_CS_NEA__Suvat_Solver_.Models;
using AQA_A_Level_CS_NEA__Suvat_Solver_.Data;

namespace AQA_A_Level_CS_NEA__Suvat_Solver_.Pages.UserLogin
{
[BindProperties(SupportsGet = true)]
public class RegisterPageModel : PageModel
{

    public new TempUserLoginModel TempUser { get; set; }
    public bool HasPassword { get; set; } = true;
    public bool HasUsername { get; set; } = true;
    public bool UniUsername { get; set; } = true;
    public bool RegisterApproved { get; set; } = false;
    public bool AQAPhys { get; set; } = false;
    public bool AQAMaths { get; set; } = false;
    public bool SubjectChosen { get; set; } = true;

    private readonly ApplicationDbContext _context;
    public RegisterPageModel(ApplicationDbContext context)
    {
        _context = context;
    }
    public List<User> UserList = new List<User>();

    public void OnGet()
    {

    }
    public IActionResult OnPost()
    {
        User User = new User();
        HasPassword = true;
        HasUsername = true;
        UniUsername = true;
        SubjectChosen = true;
        UserList = _context.User.ToList();

        if (!AQAMaths && !AQAPhys)
        {
            SubjectChosen = false;
        }
        if (string.IsNullOrWhiteSpace(TempUser.Password) || TempUser.Password.Length < 4)
        {
            HasPassword = false;
        }
        if (string.IsNullOrWhiteSpace(TempUser.Username) || TempUser.Username.Length < 4)
        {
            HasUsername = false;
        }
        if (TempUser.Username == //database UserName value here )
        {
            //Here would be where the Username is compared
            //UniUsername = false;
        }
        if (!HasPassword || !HasUsername || !UniUsername || !SubjectChosen)
        {
            return RedirectToPage("/UserLogin/RegisterPage", new { HasPassword, HasUsername, UniUsername, SubjectChosen });
        }
        else
        {
            RegisterApproved = true;
            User.UserName = TempUser.Username;
            User.UserPass = TempUser.Password;
            User.UserCorrectAnsw = 0;
            User.UserTotalAnsw = 0;
            _context.User.Add(User);
            _context.SaveChanges();
            return RedirectToPage("/UserLogin/LoginPage", new { RegisterApproved });
        }
    }
}
}

Many Thanks.

CodePudding user response:

Probably the strongest method is to enforce the user name column to be unique at the database level using a Unique Constraint. That way if you try to add a user with a duplicate user name, the database will simply return an error.

This article shows how to create a Unique Constraint with Entity Framework

You can be sure that the database will not allow a user with a duplicate user name with this method. However, trying to add a duplicate user will create an error which you will have to either handle or prevent from occurring in the first place (which is what you are doing now)

So for the code you are using now, since you already have the users pulled from the database here:

UserList = _context.User.ToList();

We can use LINQ to check if any of the users Usernames in UserList matches the TempUser like this:

if (UserList.Any(x => x.Username == TempUser.Username))
{
    //Here would be where the Username is compared
    UniUsername = false;
}

Since you didn't share your User model, this assumes your User class has a property named Username.

Happy Coding

  • Related