ASP.NET MVC 模型绑定字节数组

3

我需要接收来自模型的字节数组并正确绑定它。这不是针对文件的操作,而是为了保护字符串中的敏感信息免受常驻内存的风险。以下是我的模型类。这是一个比较大的模型,我不想为此分别编写一个动作。

public class ParentModel 
{
    //a lot of properties
    public SensitiveData Sensitive { get; set; }
}

public class SensitiveData
{
    public byte[] Value { get; set; }
}

返回的响应将会是:
public class ParentViewModel 
{
    //a lot of properties
    public SensitiveDataViewModel Sensitive {get;set;}
}

public class SensitiveDataViewModel
{
    public int Id { get; set; }
    public string Maksed { get; set; }
    public string Hash { get; set; }
}

我理解这可能是一个多部分上传,但是字节数组最多只有32个字符,并且它不是文件。如果它是一个字符串,它将以明文形式被固定在内存中。我无法使应用程序完全防弹,但我希望数据能够尽快清除,这样就更难获取这些数据并减少它在内存中存在的时间。如果MVC将整个请求放入一个字符串中,我对此无能为力。

您是否考虑过加密这些信息,而不是将其分离为字节?并且只在需要使用信息时才进行解密。 - Marko
1
你有什么问题?只发布你的模型的一部分,不能指望别人能够帮助你。 - ataravati
2
安全字符串呢? - Phil Degenhardt
我将加密这些信息,但首先需要有人将其发送给我。我想能够使用SecureString,但如果它作为字符串传递到模型中,它仍然会在进入SecureString之前被整合。在获取值后,我将返回一个ID、掩码和哈希值。 - Josh
问题到底是什么?你的 byte 数组没有正确绑定吗? - James
1个回答

0

解决这个问题的一种方法是使用CompilationRelaxationsAttribute:

http://msdn.microsoft.com/en-US/library/system.runtime.compilerservices.compilationrelaxationsattribute.aspx

来自 msdn 文档 (http://msdn.microsoft.com/library/system.string.intern.aspx):

.NET Framework 2.0 版本引入了 CompilationRelaxations.NoStringInterning 枚举成员。NoStringInterning 成员将程序集标记为不需要字符串字面量的内部化。您可以使用 CompilationRelaxationsAttribute 属性将 NoStringInterning 应用于程序集。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接