首页 > 解决方案 > 当存储密码然后面对错误?

问题描述

我将密码加密格式存储在表中,并在密码的模型类属性中赋值,但给出错误?

验证失败的一个或多个属性。

查看我的监视窗口

家庭控制器.cs

        public static string Encrypt(string clearText)
        {
            try
            {
                byte[] hashBytes = ComputeHash(clearText);
                byte[] saltBytes = GetRandomSalt();
                byte[] saltHash = ComputeHash(saltBytes.ToString());

                byte[] hashWithSaltBytes = new byte[hashBytes.Length + saltBytes.Length];
                for (int i = 0; i < hashBytes.Length; i++)
                    hashWithSaltBytes[i] = hashBytes[i];
                for (int i = 0; i < saltBytes.Length; i++)
                    hashWithSaltBytes[hashBytes.Length + i] = saltBytes[i];

                string hashValue = Convert.ToBase64String(hashWithSaltBytes);

                return hashValue;
            }
            catch (Exception)
            {

                throw;
            }
        }


        public static byte[] GetRandomSalt()
        {
            int minSaltSize = 16;
            int maxSaltSize = 32;

            Random random = new Random();
            int saltSize = random.Next(minSaltSize, maxSaltSize);
            byte[] saltBytes = new byte[saltSize];
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            rng.GetNonZeroBytes(saltBytes);
            return saltBytes;
        }

        public static byte[] ComputeHash(string plainText)
        {
            byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
            HashAlgorithm hash = new SHA256Managed();
            return hash.ComputeHash(plainTextBytes);
        }

        public ActionResult create()
        {
            return View();
        }

        public ActionResult create(student stud)
        {
            try
            {
                string pass = Encrypt(stud.password);
                stud.password = pass; //assigning a string pass to stud.pass

                var create = dbstud.students.Add(stud);
                dbstud.SaveChanges();
                return RedirectToAction("Login");

            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
        }

学生.cs

    public partial class student
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public student()
        {
            this.blogs = new HashSet<blog>();
        }

        public int studid { get; set; }
        public string firstname { get; set; }
        public string lastname { get; set; }
        public string username { get; set; }
        public string password { get; set; }
        public string email { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<blog> blogs { get; set; }
    }
dbstud.SaveChanges();     //here give an error validation failed one or more entities.

查看观察窗口哪个地方出现此错误

现在我正在存储密码加密格式并在密码的模型类属性中分配值但给出错误?

如何解决这个错误?

password varchar(50)

当我输入密码 1 运行时然后给出错误:看到这个图片

在此处输入图像描述

标签: asp.net-mvc

解决方案


从聊天中潜在的错误是

"The field password must be a string or array type with a maximum length of '50'." string //this is the error

该错误意味着您的密码数据库列不允许长度超过 50 个字符的字符串,并且尝试保存学生时密码的值超过 50 个字符。

您需要取消列的最大字符串长度,或增加它,以便“加密”密码长度永远不会超过它。

要更改列的 varchar 最大长度,您可以执行类似的操作(我在这里猜测您的表名)。这将完全消除该列的最大长度限制:

ALTER TABLE [student] ALTER COLUMN [password] VARCHAR(MAX)

有关其他信息,请参阅https://stackoverflow.com/a/8829066/493557https://stackoverflow.com/a/2696003/493557


推荐阅读