[英]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选项是
public async Task<User> GetUser(string email, string password)
{
return await _context.Set<User>()
.Where(e => e.Email == email
&& e.Password == YourEncodeMethod(password))
.FirstOrDefaultAsync();
}
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;
}
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.