RavenDB 查询以检查用户名和密码

发布于 2024-12-03 06:26:03 字数 261 浏览 0 评论 0原文

我对 C# 和 RavenDB 相当陌生,所以请原谅我缺乏理解。

我目前有一个 Windows 窗体应用程序。在其中一种表单中,我有两个文本框和一个按钮。这两个文本框用作用户名和密码输入,按钮显然在那里,以便用户可以登录。当用户单击按钮时,将调用一个方法并将两个输入的内容保存在两个字符串变量中。

目前,在我的 RavenDB 数据库中,我创建了两个用户名和密码示例。

如何正确检查用户提供的用户名和密码是否存在于数据库中。

非常感谢任何帮助。

I'm fairly new to C# and RavenDB, so please excuse my lack of understanding.

I currently have a Windows Form Application. In one of the forms, I have two text boxes and one button. These two text boxes serve as the username and password inputs and the button is obviously there so that the user can login. When the user clicks on the button, a method is called and saves the content of the two inputs in two string variables.

At the moment, in my RavenDB Database, I have created two samples of username and password.

How do I appropriately check whether the username and password given from the user exists in the database.

Any help is really appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一影成城 2024-12-10 06:26:03

有两种方法可以回答这个问题。

a) 您可以使用 Linq 提供程序查询多个属性

session.Query<User>().Where(user=> user.Name = username && user.Password = pass).ToList();

b) 这样做的问题是,这假设您将密码以纯文本形式存储在数据库中,而您绝不应该这样做。
您可以在 RaccoonBlog 的 RavenDB 示例应用程序中看到我们如何实现这一点:

https://github.com/ayende/RaccoonBlog/blob/master/src/RaccoonBlog.Web/Models/User.cs

https://github.com/ayende /RaccoonBlog/blob/master/RaccoonBlog.Web/Areas/Admin/Controllers/LoginController.cs

There are two ways to answer this question.

a) You can query for multiple properties using the Linq provider

session.Query<User>().Where(user=> user.Name = username && user.Password = pass).ToList();

b) The problem with this is that this assumes that you are storing the password as plain text in the database, which you should never do.
You can see how we implemented that in RaccoonBlog's RavenDB's sample application:

https://github.com/ayende/RaccoonBlog/blob/master/src/RaccoonBlog.Web/Models/User.cs

https://github.com/ayende/RaccoonBlog/blob/master/RaccoonBlog.Web/Areas/Admin/Controllers/LoginController.cs

黯然 2024-12-10 06:26:03

作为良好的安全实践,您根本不存储密码,而是存储密码的哈希值。

存储您的密码

  1. 读取服务器上的值并生成密码的哈希码。您应该使用加密函数来生成哈希(例如通过 SHA256 )

  2. 将文档存储在 User 类型的 Raven DB 中,并包含用户名和哈希密码

检查用户是否传递的凭据是在数据库中

  1. 查询 Raven DB 并查找具有给定名称和密码哈希的用户。

示例代码

var user = session.Query<User>()
    .Where(u => u.UserName == "Alice" && u.HashedPassword == "hashPwd");

As a matter of good security practice you don't store passwords at all, rather you you store the password's hash.

To store your password

  1. Read the values on the server and generate a hashcode of the password. You should use crypto functions to generate hash (such as via SHA256)

  2. Store a document in Raven DB of type User with his username and hashed password

To check if the user with the passed credentials is in the database

  1. Query Raven DB and look for the user with the given name and password hash.

Sample code

var user = session.Query<User>()
    .Where(u => u.UserName == "Alice" && u.HashedPassword == "hashPwd");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文