简体   繁体   English

使用 MongoDB C# (ASP.NET) 的登录表单

[英]Login form using MongoDB C# (ASP.NET)

I'm having difficulty with the login part.我在登录部分遇到困难。

I have added a textbox ( txtresult ) in Login part to see what I'm getting and the result is我在登录部分添加了一个文本框( txtresult )来查看我得到了什么,结果是

find({ "Email": "the email I've added" }) find({ "Email": "我添加的 email" })

I also need an if statement to complete the login form and go to another page.我还需要一个 if 语句来完成登录表单和 go 到另一个页面。

Register code behind (works fine)在后面注册代码(工作正常)

protected void btnSignUp_Click(object sender, EventArgs e)
{
        var Connection = "mongodb://localhost";
        var client = new MongoClient(Connection);
        var Db = client.GetDatabase("bookstore_db");
        var Collection = Db.GetCollection<UserModel>("User");

        UserModel db = new UserModel();

        db.FirstName = txtFirstName.Text.ToString();
        db.LastName = txtLastName.Text.ToString();
        db.Email = txtEmail.Text.ToString();
        db.Password = txtPassword.Text.ToString();

        Collection.InsertOneAsync(db);
        Response.Redirect("index.aspx");
}

There are several issues in your code:您的代码中有几个问题:

  1. You are creating a new MongoClient for each request, and that is not correct.您正在为每个请求创建一个新的MongoClient ,这是不正确的。 You should create a MongoClient singleton and use always the same one.您应该创建一个MongoClient singleton 并始终使用相同的。

  2. You do not have separation of concerns, you should not have database logic in your controllers, instead, you should abstract all that logic in another class, eg, IUserRepository您没有关注点分离,您的控制器中不应该有数据库逻辑,相反,您应该在另一个 class 中抽象所有逻辑,例如IUserRepository

  3. You should NEVER store the user password in plain text as you are trying to do.切勿像尝试那样以纯文本形式存储用户密码。 That leads to huge security problems, instead you should always hash the password and only then store the hash.这会导致巨大的安全问题,相反,您应该始终使用 hash 密码,然后才存储 hash。 Microsoft documentation on how to hash passwords.有关如何使用 hash 密码的Microsoft 文档

  4. Database operations should be asynchronous, so you should return Task.数据库操作应该是异步的,所以你应该返回 Task。 Async/await Official Documentation异步/等待官方文档

I have refactored a little your code, use it as an example, and work on that:我对您的代码进行了一些重构,将其用作示例,然后进行处理:

UserRepository用户存储库

public interface IUserRepository
{
    Task CreateUser(UserModel user);
}

public class UserRepository : IUserRepository
{
    private readonly IMongoCollection<UserModel> _collection;

    public UserRepository()
    {
        const string connection = "mongodb://localhost";
        var mongoClient = new MongoClient(connection);
        IMongoDatabase? database = mongoClient.GetDatabase("bookstore_db");
        _collection = database.GetCollection<UserModel>("User");
    }

    public Task CreateUser(UserModel user)
    {
        return _collection.InsertOneAsync(user);
    }
}

UserController用户控制器

public class UserController : Controller
{
    private readonly IUserRepository _userRepository;

    public UserController(IUserRepository userRepository)
    {
        _userRepository = userRepository;
    }
    
    protected async Task RegisterUser(object sender, EventArgs e)
    {
        UserModel user = new UserModel
        {
            FirstName = txtFirstName.Text.ToString(),
            LastName  = txtLastName.Text.ToString(),
            Email     = txtEmail.Text.ToString(),
            Password  = txtPassword.Text.ToString()
        };
        await _userRepository.CreateUser(user);
        Response.Redirect("index.aspx");
    }
}

This solves a few of the code problems, and it also adds asynchronism, which improves performance.这解决了一些代码问题,还增加了异步性,从而提高了性能。

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

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