简体   繁体   English

System.InvalidOperationException:LINQ 表达式....'无法翻译。 ASP.NET 6...方法“DecodeFrom64”的翻译失败

[英]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.在 ASP.NET Core 6 Web API 项目中,我试图在将密码与输入的密码进行比较之前从数据库中检索和解码密码。

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:我收到此错误:

 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期望从数据库中检索和解码密码并与用户输入的密码进行比较

EF translates LINQ (when working with IQueryable ) into SQL and it does not know anything about your method DecodeFrom64 and can't translate it. EF 将 LINQ(使用IQueryable时)翻译成 SQL,它对您的方法DecodeFrom64 ,也无法翻译它。 Options are选项是

  • Encoding password and checking it server-side (db side):编码密码并在服务器端(数据库端)检查它:
public async Task<User> GetUser(string email, string password)
{
     return await _context.Set<User>()
          .Where(e => e.Email == email
                    && e.Password == YourEncodeMethod(password))
          .FirstOrDefaultAsync();
}
  • Fetching the user by email (I suppose it should be unique) and check password client-side.通过 email 获取用户(我想它应该是唯一的)并在客户端检查密码。 Something along this lines:沿着这条线的东西:
public async Task<User> GetUser(string email, string password)
{
     var user = await context.Set<User>()
          .FirstOrDefaultAsync(e => e.Email == email);
     if(user is not null && DecodeFrom64(user.Password) == password)
     {
          return user;
     }
     return null;
}
  • Implementing the decode function on the db side and mapping it .在数据库端实现解码 function 并将其映射

But in general you should consider storing password hashes ( see this question ), not the encoded ones and check them.但总的来说,您应该考虑存储密码哈希值(请参阅此问题),而不是编码的哈希值并检查它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET 核心 Web API - System.InvalidOperationException: LINQ 表达式 'DbSet<mandate> ()</mandate> - ASP.NET Core Web API - System.InvalidOperationException: The LINQ expression 'DbSet<Mandate>() System.InvalidOperationException:无法使用带有 PostgreSQL 的 EF Core 翻译 LINQ 表达式 - System.InvalidOperationException: The LINQ expression could not be translated using EF Core with PostgreSQL LINQ 表达式 'DbSet<> 无法转换方法 'System.DateTime.ToString' 失败,从 ASP.NET Core 2.1 迁移到 .NET6 后 - The LINQ Expression 'DbSet<> Could not be translated method 'System.DateTime.ToString' failed, After Migrated from ASP.NET Core 2.1 to .NET6 ASP.Net 错误 System.InvalidOperationException - ASP.Net error System.InvalidOperationException System.InvalidOperationException:LINQ 表达式 - System.InvalidOperationException: The LINQ expression Asp.net 内核 REST API Z979E08FD891EBB5526A70C82D741A40Z 表达式不能翻译 - Asp.net Core REST API The LINQ expression Could not be Translated 得到了System.InvalidOperationException:方法失败,出现意外错误代码64 - Got System.InvalidOperationException: Method failed with unexpected error code 64 错误:ASP.NET WebAPI中的System.InvalidOperationException - Error: System.InvalidOperationException in ASP.NET WebAPI System.InvalidOperationException: LINQ 表达式 'GroupByShaperExpression: - System.InvalidOperationException: The LINQ expression 'GroupByShaperExpression: ASP.NET Web服务:身份验证失败。 ExceptionType“:” System.InvalidOperationException” - ASP.NET WebService: Authentication failed. ExceptionType“:”System.InvalidOperationException"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM