none
.net mvc后台给Model绑定了一个解密后的int属性值,传到前台保存后,html中变为解密前的字符串 RRS feed

  • 问题

  • 如图一,后台接收到一个加密字符串,解密后转为int值绑定到Model上,通过监视模版能看到绑定值是正常的

    如图二,传递到前台时,通过断点监视模版能看到仍然是正常的int值

    如图三,但是在生成的html中,能看到保存的并不是int值,而是加密前的字符串值

    请问,这是什么原因导致的?

    引发的情况,我使用Ajax.BeginForm生成的表单,因为该值变为了字符串形式而非int数字,导致在提交的时候自动检测未通过,提交被拒绝。

    2019年9月29日 8:42

全部回复

  • 你好,

    我根据你的代码做了一个简单的测试,并没有重现你的问题,我建议你把整个的controller代码包括加密和解密的部分已经整个view的代码都贴出来给我们看下,这样可以方便我们重现并且找到问题。

    我的测试代码如下:

    Controller:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    using System.Web;
    using System.Web.Mvc;
    
    namespace MVCNormallIssue.Controllers
    {
        public class EncryptValueController : Controller
        {
            // GET: EncryptValue
            public ActionResult Index()
            {
    
                TestModel model = new TestModel();
    
                string encryptstr = Encrypt("30");
    
                string decryptstr = Decrypt(encryptstr);
                model.IndicatorID = Convert.ToInt32(decryptstr);
    
    
    
    
    
    
                return View(model);
            }
    
            private string Encrypt(string clearText)
            {
                string EncryptionKey = "MAKV2SPBNI99212";
                byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
                using (Aes encryptor = Aes.Create())
                {
                    Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                    encryptor.Key = pdb.GetBytes(32);
                    encryptor.IV = pdb.GetBytes(16);
                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                        {
                            cs.Write(clearBytes, 0, clearBytes.Length);
                            cs.Close();
                        }
                        clearText = Convert.ToBase64String(ms.ToArray());
                    }
                }
                return clearText;
            }
    
            private string Decrypt(string cipherText)
            {
                string EncryptionKey = "MAKV2SPBNI99212";
                byte[] cipherBytes = Convert.FromBase64String(cipherText);
                using (Aes encryptor = Aes.Create())
                {
                    Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                    encryptor.Key = pdb.GetBytes(32);
                    encryptor.IV = pdb.GetBytes(16);
                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                        {
                            cs.Write(cipherBytes, 0, cipherBytes.Length);
                            cs.Close();
                        }
                        cipherText = Encoding.Unicode.GetString(ms.ToArray());
                    }
                }
                return cipherText;
            }
        }
    
    
        public class TestModel
        {
      
            public int IndicatorID { get; set; }
        }
    
    
    
    }

    View:

    @model MVCNormallIssue.Controllers.TestModel
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
        <div> 
    
            @using (Ajax.BeginForm("","",new AjaxOptions { HttpMethod= "post", UpdateTargetId="itemcontent", OnBegin="estarshow" }))
    
    
            {<div class="item_content">
            @Html.HiddenFor(m => m.IndicatorID)
    
            </div>
    
            }
        </div>
    </body>
    </html>
    

    图片:


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年9月30日 2:13