简体   繁体   English

如何在 LINQ 查询中使用自定义方法

[英]How to use custom Methods in LINQ Query

I have the following issue: I am currently working on a ASP .net core entity framework backend and have the problem that I need to use a Custom Method in LINQ Query and getting a error when doing this.我有以下问题:我目前正在处理 ASP .net 核心实体框架后端,并且遇到问题,我需要在 LINQ 查询中使用自定义方法,并且在执行此操作时出现错误。 I researched and found out that it is possible to write custom functions, that will be translated to sql, but I think that there is not a big scope for doing this.我研究并发现可以编写自定义函数,将其转换为 sql,但我认为没有大的 scope 可以这样做。 (eg: SQL will not be able to use Libaries and hash strings). (例如:SQL 将无法使用库和 hash 字符串)。

Another way that I have heard of ist to convert my Database to a Enumberale and then apply my Custom Methods on it, which works, but is not that performant, because I am saving my whole Database in my memory, which gets very slow when having a huge amount of data.我听说过的另一种方法是将我的数据库转换为 Enumberale,然后在其上应用我的自定义方法,这种方法有效,但性能不佳,因为我将整个数据库保存在我的 memory 中,当有大量的数据。 So my question is, if there is a performant solution to perform custom methods in LINQ queries?所以我的问题是,在 LINQ 查询中是否有执行自定义方法的高性能解决方案?

My detailed problem is, that I have saved my salted passwords hashed in my database and when someone want s to log in to his account I have to compare the password in the database with the salt + user password input, that has to get hashed in my where clause.我的详细问题是,我已经将我的加盐密码保存在我的数据库中,当有人想要登录他的帐户时,我必须将数据库中的密码与盐 + 用户密码输入进行比较,这必须得到散列我的 where 子句。 It would work if I wouldnt use salts, because then, I would only have to hash the user input, which is not column of the database.如果我不使用盐,它会起作用,因为那时我只需要 hash 用户输入,这不是数据库的列。

What you should do is - calculate the hash and salt in the backend, and use the computed hash in your WHERE statement.您应该做的是 - 在后端计算 hash 和盐,并在 WHERE 语句中使用计算出的 hash。 In this case you don't need to call your methods from SQL equally you don't need to pull entire db (or table) into memory to compute hash.在这种情况下,您不需要从 SQL 调用您的方法,同样您也不需要将整个数据库(或表)拉入 memory 来计算 hash。

As I don't know your code, the pseudo-code approach would be:由于我不知道您的代码,因此伪代码方法是:

    var user = service.GetUserByEmail(email);
if (user == null) {
//Invalid User
}
    var hash = ComputeHash(user.Salt, inputPwd);
    
 if(user.PasswordHash == hash) {
//User is logged in
} else {
//Invalid Password or email
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM