Can anyone help with some problems I have. I am trying to create an self made authentication method, im abit stuck in a few areas and was hoping some one could help. First thing I would like to ask is how to solve the problem I have commented in the code:
public string Authentication(string studentID, string password)
{
var result = students.FirstOrDefault(n => n.StudentID == studentID);
//find the StudentID that matches the string studentID
if (result != null)
//if result matches then do this
{
//----------------------------------------------------------------------------
byte[] passwordHash = Hash(password, result.Salt);
string HashedPassword = Convert.ToBase64String(passwordHash);
//----------------------------------------------------------------------------
// take the specific students salt and generate hash/salt for string password (same way student.Passowrd was created)
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
byte[] UserPassword = enc.GetBytes(HashedPassword);
UserPassword.SequenceEqual(result.Password); // byte[] does not contain a definition for SequenceEqual?
//check if the HashedPassword (string password) matches the stored student.Password
}
return result.StudentID;
//if string password(HashedPassword) matches stored hash(student.Passowrd) return student list
//else return a message saying login failed
}
"Cannot be used like a method" is likely because you added brackets: result.Password()
if it is a property drop the brackets result.Password
. Adding the brackets makes the compiler attempt to compile it as a method call when in fact it is a property or field.
The second error is you are trying to return students
, which is a list of students. The method requires a string
as a return value. Did you mean to instead return result.StudentID;
? The exception is detailing the failed attempt at compiling the cast from List<Student>
to string
.
I cannot offer any advice on the second half of your questions.
Update
You are expected to find a method called SequenceEqual
on byte[]
. This is a Linq extension method so you may need to add:
using System.Linq;
To the top of your file.
You will then likely get an error trying to pass a string to this method: SequenceEqual(result.Password);
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With