Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

System.InvalidOperationException: The LINQ expression…. 'could not be translated. Asp.Net 6 …Translation of method 'DecodeFrom64' failed

In ASP.NET Core-6 Web API Project, I’m trying to retrieve and decode the password from the database before comparing it with the entered password.

I have this code:

public async Task<User> GetUser(string email, string password)
{
     return await _context.Set<User>().Where(e => e.Email == email
                    && DecodeFrom64(e.Password) == password).FirstOrDefaultAsync();
}

I got this error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

System.InvalidOperationException: The LINQ expression 'DbSet<User>()
    .Where(u => u.Email == __email_0 && 
DbHelper.DecodeFrom64(u.Password) == __password_1)'
could not be translated. Additional information: 
Translation of method 'UserOnboardingApi.Model.DbHelper.DecodeFrom64' > failed.

How do I get this resolved?

Thanks

Expected to retrieve and decode the password from the database and compare to what the user entered

>Solution :

Here is your solution

public async Task<User> GetUser(string email, string password)
{
  Func<User, bool> isValidUser = (User user) => {
                return user.Email == email &&
                   DecodeFrom64(user.Password) == password;
            };
     return await _context.Set<User>().Where(e => isValidUser(e)).FirstOrDefaultAsync();
}

Or your can encode the password before passing and do not decode user.Password

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading