。NET Core中存储模型的存储过程

问题描述 投票:1回答:1

我有一个具有登录屏幕的项目。当用户登录时,Controller会调用另一个我正在用作数据访问类的类。该类称为存储过程,以验证用户是否有效,并拉回所有用户信息并将其加载到UserModel中。当我只有数据访问方法返回布尔值时,此方法有效。因此,我已经登录。但是该信息不会保留或永远不会存储在我的模型中。我已经对此进行了研究,并为此进行了数周的研究。我希望另一双眼睛可以帮助我了解我所缺少的东西。谢谢。

这是我的模特

public class UserModel
    {
        /// <summary>
        /// Holds the current username.
        /// </summary>
        public string UserName {get;set;}

        /// <summary>
        /// Holds the First Name of the user.
        /// </summary>
        public string FirstName { get; set; }

        /// <summary>
        /// Holds the last name of the user.
        /// </summary>
        public string LastName { get; set; }

        /// <summary>
        /// Holds the users Date of Birth.
        /// </summary>
        public string UserDOB { get; set; }

        /// <summary>
        /// Holds the users Email Address 
        /// that was provided.
        /// </summary>
        public string Email { get; set; }

        /// <summary>
        /// Holds the current Password of the user.
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// Holds the Date the account was created.
        /// </summary>
        public DateTime CreationDate { get; set; }

        /// <summary>
        /// Holds the PK of the type of user,
        /// Admin, Standard, Promotional
        /// </summary>
        public int UserType { get; set; }
    }

这是我的数据访问权限

public bool UserLogin(string userName, string Password)
        {
            bool userFound = false;

            const string Query = @"GetUserLogin";

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();

                SqlCommand command = new SqlCommand(Query, connection)
                {
                    CommandType = CommandType.StoredProcedure
                };
                command.Parameters.AddWithValue("@UserName", userName);
                command.Parameters.AddWithValue("@Password", Password);

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    reader.Read();
                    userModel.UserName = reader["UserName"].ToString();
                    userModel.FirstName = reader["FirstName"].ToString();
                    userModel.LastName = reader["LastName"].ToString();
                    userModel.Email = reader["Email"].ToString();
                    userModel.UserDOB = reader["UserDOB"].ToString();

                    if (reader == null)
                    {
                        userFound = false;
                    }
                    else
                    {
                        userFound = true;
                    }
                }
            }

            return userFound;
        }

这是我的用户控制器

    public class UserController : Controller
    {
        private UserLoginDataAccess dataAccess = new UserLoginDataAccess();

        // GET: /Login/
        [HttpGet]
        public IActionResult Login()
        {
            return View();
        }

        [HttpPost, ValidateAntiForgeryToken]
        public IActionResult Login(UserModel model)
        {
            bool isLoggedIn;

            isLoggedIn = dataAccess.UserLogin(model.UserName, model.Password);

            if (isLoggedIn)
            {
                return View("Dashboard");
            }
            else
            {
                return View();
            }
        }
    }
html .net .net-core
1个回答
1
投票

首先,这绝不能为空:

if (reader == null)

第二:userModel在哪里/如何定义?如果那是控制器上的一个字段,请注意,它在请求之间没有持久存在。您需要在这里使用其他方法。通常是某种身份验证cookie。

[第三:我很震惊地知道您是否正在数据库中进行统一密码比较;请让它散列...

第四:编写自己的安全代码几乎绝不是一个好主意。有一些登录提供程序可以正确解决此问题。我建议使用它们。

第五:您应该测试Read()是否返回true-尽管坦率地说,我认为我建议将所有这些数据库代码卸载到Dapper之类的东西。

© www.soinside.com 2019 - 2024. All rights reserved.