关于为隐藏字段编写自定义 html 帮助程序,ASP.Net MVC 几乎没有好的功能



这样我就可以为隐藏字段编写自定义助手

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Linq.Expressions;
namespace CustomHtmlHelpers.CustomHelpers
{
    public static class CustomHiddenHelperModelBinding
    {
        //This overload accepts single expression as parameter.
        public static MvcHtmlString Custom_HiddenFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression)
        {
            return Custom_HiddenFor(helper, expression, null);
        }
        //This overload accepts expression and htmlAttributes object as parameter.
        public static MvcHtmlString Custom_HiddenFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
        {
            //Fetching the metadata related to expression. This includes name of the property, model value of the property as well.
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
            string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
            //Fetching the property name.
            string propertyName = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
            //Creating a textarea tag using TagBuilder class.
            TagBuilder hidden = new TagBuilder("input");
            //Setting the type attribute to hidden to render hidden input field.
            hidden.Attributes.Add("type", "hidden");
            //Setting the name and id attribute.
            hidden.Attributes.Add("name", propertyName);
            hidden.Attributes.Add("id", propertyName);
            //Setting the value attribute of textbox with model value if present.
            if (metadata.Model != null)
            {
                hidden.Attributes.Add("value", metadata.Model.ToString());
            }
            //merging any htmlAttributes passed.
            hidden.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            return MvcHtmlString.Create(hidden.ToString(TagRenderMode.Normal));
        }
    }
}

稍后我们可以像

@Html.Custom_HiddenFor(Model => Model.hidden)
@Html.Custom_HiddenFor(Model => Model.hidden, new { @class = "hiddenClass" })

我的目标是为隐藏字段重写我自己的 HTML 助手,以便在客户端将值呈现为加密文本和防篡改。如果有人篡改数据,那么我想在服务器端检查,如果篡改,那么我将向用户显示友好的错误消息。

这是另一个使用机器密钥加密的 SAMPE 代码,但我不确定该代码在部分信任环境中是否正常工作?

这是示例代码

string Protect(byte[] data)
{
    if (data == null || data.Length == 0) return null;
    return MachineKey.Encode(data, MachineKeyProtection.All);
}
byte[] Unprotect(string value)
{
    if (String.IsNullOrWhiteSpace(value)) return null;
    return MachineKey.Decode(value, MachineKeyProtection.All);
}

 here’s the 4.5 usage (it supports a slightly more sophisticated usage)
 -------------------------------------------------------------------------
 const string MachineKeyPurpose = "MyApp:Username:{0}";
 const string Anonymous = "<anonymous>";
 string GetMachineKeyPurpose(IPrincipal user)
 {
     return String.Format(MachineKeyPurpose,
         user.Identity.IsAuthenticated ? user.Identity.Name : Anonymous);
 }
 string Protect(byte[] data)
 {
     if (data == null || data.Length == 0) return null;
     var purpose = GetMachineKeyPurpose(Thread.CurrentPrincipal);
     var value = MachineKey.Protect(data, purpose);
     return Convert.ToBase64String(value);
 }
 byte[] Unprotect(string value)
 {
     if (String.IsNullOrWhiteSpace(value)) return null;
     var purpose = GetMachineKeyPurpose(Thread.CurrentPrincipal);
     var bytes = Convert.FromBase64String(value);
     return MachineKey.Unprotect(bytes, purpose);
}

另一种加密方式

To generate a random string, use the RNGCryptoServiceProvider.
public string GenerateSalt(int length)
{
    var rng = new RNGCryptoServiceProvider();
    var buffer = new byte[length];
    rng.GetBytes(buffer);
    return Convert.ToBase64String(buffer);
}

现在我们可以使用以下函数生成散列密码

public virtual string CreatePasswordHash(string password, string saltkey, string passwordFormat = "SHA1")
{
    if (String.IsNullOrEmpty(passwordFormat))
        passwordFormat = "SHA1";
    string saltAndPassword = String.Concat(password, saltkey);
    string hashedPassword =
        FormsAuthentication.HashPasswordForStoringInConfigFile(
            saltAndPassword, passwordFormat);
    return hashedPassword;
}

因此,请指导我如何重写自己的自定义HTML助手,该助手将以最安全的方式加密数据,然后可以检查以确保值是否在客户端被篡改。

我还有另一个要求,即在表单发布和调用操作方法时轻松解密值。我想使用操作方法上的属性解密加密值。我想触发一个函数将在操作方法和解密之前触发数据反序列化为模型或操作方法参数之前的值。

可能吗?

我希望我的操作方法看起来像

 [HttpPost]
 [Decrypt]
 public ActionResult Save(string personname, string email)
 {
       return View();
 }
 or
 [HttpPost]
 [Decrypt]
 public ActionResult Save(Person oPerson)
 {
        return View();
 }

我希望我的 [Decrypt] 属性将触发一个调用 decypt 的方法,并在操作方法 Save 调用之前将所有值传递给 decypt(( 函数。 如果 decypt(( 函数找到任何加密值,那么它将解密并反序列化解密值到模型或操作方法参数。

所以我有三个要求


1(我想编写一个自定义的HTML助手,它将呈现具有加密值的隐藏字段。

2(当数据发布到服务器时,我可以检测到该值是否已被篡改。 如果篡改,则将向用户显示带有正确消息的错误页面。

3( 操作方法将具有自定义属性,该属性将在操作方法执行之前调用函数。 如果找到任何加密值,该函数将解密值。解密后,解密后的值将被正确反序列化为模型 或其他操作方法参数正确。

我是MVC的新手。 因此,请指导我应该如何继续使用示例代码。 谢谢

@Html.AntiForgeryToken()呢?

"生成一个隐藏的表单字段(防伪令牌(,该字段在提交表单时进行验证。"开发者网络

例:1.在标记中,将@Html.AntiForgeryToken()添加到表单中。2. 添加[ValidateAntiForgeryToken]属性您的控制器操作。

开发者网络

最新更新