byte []不包含SequenceEqual身份验证方法的定义

G G*_* Gr 2 c# linq authentication rest web-services

任何人都可以帮我解决一些问题。我正在尝试创建一种自制的身份验证方法,有些地方卡住了,希望有人能提供帮助。我想问的第一件事是如何解决我在代码中注释的问题:

    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 
    }
Run Code Online (Sandbox Code Playgroud)

Ada*_*rth 5

之所以会出现“不能像方法一样使用”,是因为您添加了方括号:result.Password()如果是属性,则将方括号删除result.Password。添加方括号会使编译器尝试将其编译为方法调用,而实际上它是属性或字段。

第二个错误是您尝试返回students,这是学生列表。该方法需要a string作为返回值。您是要代替return result.StudentID;吗?唯一的例外是在细节从编译投失败的尝试List<Student>string

对于您问题的后半部分,我无法提供任何建议。

更新资料

我们希望你找到一个名为方法SequenceEqualbyte[]。这是Linq扩展方法,因此您可能需要添加:

using System.Linq;

在文件顶部。

尝试将字符串传递给此方法时,您可能会遇到错误:SequenceEqual(result.Password);