tanyaa tanyaa - 10 days ago 5
ASP.NET (C#) Question

how to compare list string parameters in a function with login input(name,pass)?

I'm building a project with asp.net and I'm new with this.
I created a list that each value has 2 strings in my controller. I want to build a login page that will check if the strings in the login page input(name,password) are equal to the ones in the list(I'm trying to create login without db).in my model I already wrote a class with string password and name.
how do I add a function in my controller with the list and compare the parameters to the input of the user?

any help will be appreciated. thank you.

Controller

static List<KeyValuePair<string, string>> users = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("dennis", "pass"),
new KeyValuePair<string, string>("kap", "111111")
// how to search in a list
};


Model

public class Login
{
[Required]
[Display(Name = "שם משתמש")]
public string userName { get; set; }
[Required]
[Display(Name = "סיסמא")]
public string userPass { get; set; }


}

VSO VSO
Answer

Here is working code you can try on https://dotnetfiddle.net/.

using System;
using System.Collections.Generic; 
using System.Linq; 

public class Program
{
    public class Login
    {
        public string userName { get; set; }
        public string userPass { get; set; }
    }

    static List<KeyValuePair<string, string>> users = new List<KeyValuePair<string, string>>
        {
            new KeyValuePair<string, string>("dennis", "pass"),
            new KeyValuePair<string, string>("kap", "111111")
        };

    public static void Main()
    {
        Login testLogin = new Login();
        testLogin.userName = "dennis";
        testLogin.userPass = "pass";    
        bool isValidUser = users.Any(user => user.Key == testLogin.userName && user.Value == testLogin.userPass ); 
        Console.WriteLine(isValidUser );
    }
}

As a side note, to me, it would be more intuitive to have a 'LoginInformation' object with a username and password field. Also, I am assuming you understand that what you are doing is absolutely not secure. Passwords must be hashed and salted.

Edit: Here is the code with the check moved out into its own method:

using System;
using System.Collections.Generic; 
using System.Linq; 

public class Program
{
    public class Login
    {
        public string userName { get; set; }
        public string userPass { get; set; }

        public bool isUserNameValid(string userName, string userPass, List< KeyValuePair<string, string> > validUsers) 
        {
            return validUsers.Any(user => user.Key == userName && user.Value == userPass ); 
        }
    }

    static List<KeyValuePair<string, string>> users = new List<KeyValuePair<string, string>>
        {
            new KeyValuePair<string, string>("dennis", "pass"),
            new KeyValuePair<string, string>("kap", "111111")
        };

    public static void Main()
    {   Login login = new Login(); 
        bool isValidUser = login.isUserNameValid("dennis", "pass", users); 
        Console.WriteLine(isValidUser);
    }
}