asp.net MVC模型绑定一个字节数组

本文关键字:一个 字节 字节数 数组 MVC net 模型 绑定 asp | 更新日期: 2023-09-27 17:50:12

我需要接受来自模型的字节数组并将其正确绑定。这不是针对文件,而是为了保护字符串中的敏感信息不被存储,从而永远留在内存中。我有以下模型类。这是一个更大的模型,我不想在这部分中有一个单独的动作。

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把整个请求放在一个字符串中,我就无能为力了

asp.net MVC模型绑定一个字节数组

解决这个问题的一个方法是使用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。nostringintern枚举成员。的nostringintern成员将程序集标记为不需要的字符串文字实习。您可以将nostringintern应用于

使用CompilationRelaxationsAttribute属性。